Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replaceAll("+"," ")

 
Patrick Muls
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Doug Wang
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try string.replace("\+"," ") or string.replace("\\+"," ")? The second one is the correct one I believe.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic