Hello, I'm trying to write a routine to concert attachements send from a HTML-form. One of the changes that need to be done is replacing all '+' by spaces. I tried using string.replace("+"," "), but got: java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 Escaping \+ doenot work either. Has anybody done this before?
I guess you used the new replaceAll method, whose signature is: public String replaceAll(String regex, String replacement) It throws PatternSyntaxException if the regular expression's syntax is invalid. Or you may use the old replace(char oldc, char newc) method. [ March 29, 2002: Message edited by: Doug Wang ]
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Hello, This works just fine for me (using JDK 1.3): String string = "+h+e+l+l+o+";
string = string.replace('+', ' ');
System.out.println(string); // prints " h e l l o " I don't know if this relates to your problem: It's important to note that the replace(char, char) returns a new String object (Strings are immutable), and so a reference must be made to that new String object in order to use it - the original String object is not changed. Also, '\+' is an illegal escape character - as your compiler would let you know. Good Luck.