• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacing Characters  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1784
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why won't this strip out the '$''s

getplarray[i].replaceAll("$","")
[ February 13, 2008: Message edited by: Steve Dyke ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I'm assuming you're working with a String.)

Notice in the API that replaceAll treats the first String argument as a Regular Expression. In that context, the '$' character has special meaning to represent the end of a line. So if you want a literal '$' in your pattern, you need to use an escape sequence of \$. And in the context of a String literal, this needs an additional backslash to avoid interpretation as a Unicode escape or character escape -- that is, \\$.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, ever since JDK 1.5 you can use the replace() method rather than replaceAll(). Despite its name, the real difference is that replace() doesn't treat the first argument as a regular expression. So it's simpler, but less powerful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!