File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Line Feeds Be Gone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Line Feeds Be Gone" Watch "Line Feeds Be Gone" New topic

Line Feeds Be Gone

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
What is an easy way of removeing line feeds "\n" from a string.
Warren Bell

Warren Bell
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Is this program something like what you were looking for?

Notice, Warren, things get tricky if you have to worry about end-of-line hyphens. For instance, in the case of "mother-\nin-law", you would want to retain the hyphen upon reconstitution. On the other hand, given "sesqui-\npedalianism", you would want the hyphen to disappear when you put the word back together.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

Here's another version:
* Immutable object that strips LineFeeds (newlines) from a String
* Sample usage:
* String s = (new LFStripper("Original\nstring\nwith\nLFs")).toString();
class LFStripper {
final String noLFstring;
public LFStripper(String s) {
StringBuffer buf = new StringBuffer(s);
for (int i = buf.length()-1; i >= 0; i--) {
if (buf.charAt(i) == '\n') {
noLFstring = buf.toString();
public String toString() {
return noLFstring;
You need to start at the end of the buffer and work your way down to avoid an index exception if any linefeeds are found and deleted.

[This message has been edited by JUNILU LACAR (edited April 01, 2001).]

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: Line Feeds Be Gone
It's not a secret anymore!