This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to replace "?" in a string

 
weishan zhang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is no any method called "replaceAll" in String class of Java API. Try this code

Hope this helps!!
Nayan.
 
weishan zhang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you for you reply but the error remains the same.
"replaceAll()" start since jdk1.4.
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a double backslash:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool, thanks Jim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic