Hi I would like to replace \r \n by <br> in a string . How do I do that ? I tried using replace but it only works when you want to replace a single character. Thanks for your help
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
posted
0
If you use SDK 1.4, it's easy:
Alternately, you can make use of the readLine() method in BufferedReader. Create a BuffereReader which reads from your input stream, and use a while loop to read each line in the string. For each line, write that line to a StringBuffer, and then also write a "<br>". The first method is easy to modify for other types of string replacement problems, while the latter method is really only good for replacing the divisions between lines. But consider - what if the input string comes from a Unix or Mac source, where instead of "\r\n" the new line separator is "\n" or "\n\r"? The first method will not work well in that case - but the second will work fine, because readLine() is designed to take these other possibilities into account. The choice is yours... [ January 22, 2002: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
nabou diack
Greenhorn
Joined: Oct 17, 2001
Posts: 13
posted
0
thanks for your reply. I tried to use replace as you said String mystring = ch1.replace ("\r\n", "<br>"); but I get this error message: facturationclient.java:105: replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String) String mystring = ch1.replace ("\r\n", "<br>");
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
posted
0
Look at my post again. This first method only works in Java 2 SDK 1.4 - the latest version of Java. [ January 23, 2002: Message edited by: Jim Yingst ]
nabou diack
Greenhorn
Joined: Oct 17, 2001
Posts: 13
posted
0
i was using sdk 1.4 but i didn't work.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
posted
0
Oops. I meant "replaceAll()", not "replace()". Sorry 'bout that.