wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to replace "?" in a string" Watch "How to replace "?" in a string" New topic
Author

How to replace "?" in a string

weishan zhang
Greenhorn

Joined: Sep 25, 2002
Posts: 8
String s="aasddd??dsfas?df";
s.replaceAll("?","");
I always got the error "java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0".
And "\?" will not work of course.
I do not want to transform to StringBuffer or char array.
Thanks,
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
Hi,
There is no any method called "replaceAll" in String class of Java API. Try this code

Hope this helps!!
Nayan.
weishan zhang
Greenhorn

Joined: Sep 25, 2002
Posts: 8
Hi,
Thank you for you reply but the error remains the same.
"replaceAll()" start since jdk1.4.
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
Hey, I'm using jdk1.3, and I tried the code just before I sent u. It worked perfectly fine. I've not used jdk1.4. So, check out all the available methods in jdk1.4 API for replacing character from a String.
-----------
Nayan.
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Use a double backslash:


Jon
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Jon is right of course. See the 1.4 API for String and follow the links to "regular expression" for more details. Basically, it turns out ? is a special character in regular expressions, and needs a preceeding \ as an escape sequence. As it happens, \ is also a special character in Java string literals, and to create a normal \ you need another preceeding \ as an escape sequence. So combining these rules you need "\\?" to represent a single ? char in a pattern. Very I know - but that's the way it is.
[ January 09, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
i found this interesting and so tried it at home on my very own computer and although it compiled, when i tried to print the output the question marks were still there...here is the bit of code -

class Print2Screen
{
public static void main (String[] args)
{
String s = "what??";
s.replaceAll("\\?","");
System.out.print(s);
}
}
is it because the reference of 's' refers to the initial value that was assigned to the String variable?


giddee up
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Strings are immutable. This means that methods that look like they should change the string are really creating new strings, which has no direct effect on the original string. So yes, s still refers to the original value "what??". To forget the old value of s and use hte replaced value instead:
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
cool, thanks Jim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to replace "?" in a string
 
Similar Threads
amount currency datatype
Looking for correct data type to take in stream of xml
Please Comment on Data Section
how to sovle these two errors~~~help~~~
SQL Data Types