Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Escaping Stuff in a String  RSS feed

 
Mike Cole
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

can somebody tell which is the simpliest way to to escape chars in a String?
Lets say i found "(si)" and want to replace it by something diffrent. But the String.replaceAll uses regex, so it will find "si" as a group. In short i need the String "(si)" translated to "\(si\)".

Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The very handy Jakarta Commons Lang library has a string replacement routine that does not use regexps, and thus does not have the special character problem: StringUtils.replace
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using JDK 5 or greater, you can use String's replace(CharSequence, CharSequence) method. While replaceFirst(String, String) and replaceAll(String, String) both use regex, replace(char, char) and replace(CharSequence, CharSequence) do not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!