File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes how to replace eol and nl Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "how to replace eol and nl" Watch "how to replace eol and nl" New topic
Author

how to replace eol and nl

nabou diack
Greenhorn

Joined: Oct 17, 2001
Posts: 13
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
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
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
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
i was using sdk 1.4 but i didn't work.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Oops. I meant "replaceAll()", not "replace()". Sorry 'bout that.
nabou diack
Greenhorn

Joined: Oct 17, 2001
Posts: 13
thanks I'll try that
 
IntelliJ Java IDE
 
subject: how to replace eol and nl
 
Threads others viewed
need to remove hyphen from textbox
Java Strings
GENERICS
How do I replace text from the any element of a list of n items.
How do I replace a single quote ( ' ) with two single quotes ( '' )
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com