File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in stringObj.replaceAll()

 
Babar Qadri
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line of code giving me errors.



I just want to replace single backslash with double. How to do that in a simple way. I have wrote a custom method to do this temporarily.

Please help me out folks.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Backslash has a special meaning in Java source code, so you have to escape it by doubling it. But backspace also has a special meaning in regular expressions, so you have to double it again. Try this:

"^(19|20)\\d{2}$".replaceAll("\\\\", "\\\\\\\\");
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using JDK 5, you can use

See also a similar discussion here.
 
Babar Qadri
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper, its working.
Thanks Jim for a valuable information.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic