File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

$ in String.replaceAll() problem

 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code does not work because of $ sign. How to make it work? Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "$" has special meaning in a replacement string. If you actually mean a "$" character, then you must escape the character...



Henry
 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,
I was trying \$ not \\$.
Why use \\ to encode $?
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bruce Jin:

Why use \\ to encode $?


This comes up quite often in regular expression work. The "\" backslash is an escape character for both the Java compiler and the regular expression compiler. If you want to tell the regular expression compiler to ignore a characters special meaning, then the regular expression compiler must see a "\" before that character. But since "\" means something to the Java compiler too, if you put just one "\" into a String it disappears (or does the wrong thing.) You must put two backslashes into a String literal to get one actual backslash character into a String. Only then will the regular expression compiler see it.
[ November 06, 2006: Message edited by: Ernest Friedman-Hill ]
 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.
Now I got it!
We are encoding \ with \\ and $ with \$.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic