• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to replace "?" in a string

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a double backslash:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool, thanks Jim
 
reply
    Bookmark Topic Watch Topic
  • New Topic