aspose file tools
The moose likes Beginning Java and the fly likes Escaping Stuff in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Escaping Stuff in a String" Watch "Escaping Stuff in a String" New topic
Author

Escaping Stuff in a String

Mike Cole
Ranch Hand

Joined: Sep 25, 2006
Posts: 38
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
Marshal

Joined: Mar 22, 2005
Posts: 35444
    
    9
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


Android appsImageJ pluginsJava web charts
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Escaping Stuff in a String
 
Similar Threads
I Can't Design to Save My Life
static initializer
which editor to use?
How to pass a long in ...Long ll = Long.valueOf(3L);//gives
How many objects created?