Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to replace eol and nl  RSS feed

 
nabou diack
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
nabou diack
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was using sdk 1.4 but i didn't work.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. I meant "replaceAll()", not "replace()". Sorry 'bout that.
 
nabou diack
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks I'll try that
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!