• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in stringObj.replaceAll()

 
Babar Qadri
Ranch Hand
Posts: 51
  • 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
Posts: 15443
41
Android IntelliJ IDE Java Scala Spring
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper, its working.
Thanks Jim for a valuable information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic