problem with replaceAll method

I have this string ; String text="hello my friend :-x";

text=text.replaceAll(":-X","<img src=file:///c:\\test\\smileys\\1.gif />");

The result is this : <img src=file:///c:textsmileys1.gif />

Why is that ?

How can i get the desired result ?

Message was edited by:

kynamar

[327 byte] By [kynamara] at [2007-11-26 23:50:20]
# 1
Try replace() rather than replaceAll().replaceAll() takes "regular expressions", so symbols like "\" have special meaning in replaceAll().
KathyMcDonnella at 2007-7-11 15:27:52 > top of Java-index,Desktop,Core GUI APIs...
# 2
Work's fine after using replace method twice!Thank's a lot KathyMcDonnel
kynamara at 2007-7-11 15:27:52 > top of Java-index,Desktop,Core GUI APIs...
# 3
Twice? That's strange.Can you post an example where using once isn't enough, but using twice is enough?
KathyMcDonnella at 2007-7-11 15:27:52 > top of Java-index,Desktop,Core GUI APIs...
# 4

I have this string ; String text="hello my friend :-x";

text=text.replace(":-X","<img src=file:///c:\\test\\smileys\\1.gif />");

The result is this : <img src=file:///c:text\smileys1.gif />

text.replace("\","\\\\");

After using this i get the result that i want

kynamara at 2007-7-11 15:27:52 > top of Java-index,Desktop,Core GUI APIs...
# 5

For best results, you should post [url=http://www.physci.org/codes/sscce/]complete, working test programs[/url], so we can confirm your results before we start trying to solve your problem. None of the code you've posted so far works the way you say it does. Here's a sample program that should clarify things for you: public class Test

{

public static void main(String[] args)

{

String text="hello my friend :-x";

System.out.println(text);

// won't work: 'X' is in wrong case

String text1 = text.replaceAll(":-X","<img src=file:///c:\\test\\smileys\\1.gif />");

System.out.println(text1);

// right case, but replaceAll eats the backslashes

String text2 = text.replaceAll(":-x","<img src=file:///c:\\test\\smileys\\1.gif />");

System.out.println(text2);

// works, but what if a user really types ":-X"?

String text3 = text.replace(":-x","<img src=file:///c:\\test\\smileys\\1.gif />");

System.out.println(text3);

// handles both cases, and satisfiies replaceAll's backslash addiction...

String text4 = text.replaceAll(":-[xX]","<img src=file:///c:\\\\test\\\\smileys\\\\1.gif />");

System.out.println(text4);

// ...but you can almost always use forward-slashes instead

// (plus, another way to deal with the case issue)

String text5 = text.replaceAll("(?i):-X","<img src=file:///c:/test/smileys/1.gif />");

System.out.println(text5);

}

}

uncle_alicea at 2007-7-11 15:27:52 > top of Java-index,Desktop,Core GUI APIs...
# 6
Sorry for that!
kynamara at 2007-7-11 15:27:52 > top of Java-index,Desktop,Core GUI APIs...