| Author |
Replacing a String
|
Nischal Tanna
Ranch Hand
Joined: Aug 19, 2003
Posts: 182
|
|
Hi guys.. I have an interesting doubt!!! I have a String str = "here's a java's guys" Suppose i want the above String as below "here\'s a java\'s guys" i.e., i want to replace ' with \'...I dont want to use StringTokenizer or StringBuffer..is it possible any other interesting way?..
|
Thnx
|
 |
Richard Scothern
Ranch Hand
Joined: May 25, 2001
Posts: 83
|
|
String.replaceAll(String, String) ? Richard
|
 |
Wayne L Johnson
Ranch Hand
Joined: Sep 03, 2003
Posts: 399
|
|
The only "tricky" thing about this case is using a back-slash (\) which has special meanings in regular expressions. Here's what you'll need: Yes, you need all four back-slashes ...
|
 |
anitha selvaraj
Greenhorn
Joined: Nov 06, 2003
Posts: 2
|
|
hi, will u plz. tell me reason, why it requires all the 4 slashes? what i thought is, only "\'" is enough to replace "'". thanks in advance
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
the backslash has special meaning when quoted in a string. it says "treat the next character as something other that what you normally do with it". so, you want a backslash and a single quote. if you just did "\'", java reads this as "hey, i have a backslash. i better do something special with the next character.... AH.. it's a single quote. i'll do something with it". I don't know exactly what it would do, but it definatly wouldn't give you what you want. so, you need at least "\\". java reads this as "hey, i have a backslash. i better do something special with the next character.... AH.. it's a backslash. what i do special with a backslash is print it (as oppposed to using it to flag the next char. as special)" in other words, to print a single backslash in a string, you need to type in 2. why you would need all four, i don't know (but in my defense, i've had no coffee yet this morning). hope that helps a little.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Wayne L Johnson
Ranch Hand
Joined: Sep 03, 2003
Posts: 399
|
|
Fred is on the right track. A single back-slash is used as an escape character, indicating to Java that the next character has special meaning. Thus "t" means the character lower-case 't', while "\t" means a tab. To get a *real* backs-slash you have to use "\\". The String.replaceAll() method uses regular expressions behind the scenes. And guess what? the back-slash has special meaning when used in regular expressions. So you have to escape the back-slash a second time. So you type in four back-slashes: "\\\\" and Java interprets that internally as a String with two back-slashes. That new string is then passed to the "replaceAll(...)" method and the regular expression engine interprets the two back-slashes as a single *real* back-slash character. That's why you need all four: because both the JVM and the regular-expression engine process the string separately, each time chopping it in half. From four to two to one.
|
 |
 |
|
|
subject: Replacing a String
|
|
|