wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Line Feeds Be Gone" Watch "Line Feeds Be Gone" New topic
Author

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.
Thanks,
Warren Bell


Warren Bell
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Warren,
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.
HTH
Art
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

Here's another version:
<pre>
/**
* 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') {
buf.deleteCharAt(i);
}
}
noLFstring = buf.toString();
}
public String toString() {
return noLFstring;
}
}</pre>
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.
J.Lacar

[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: http://aspose.com/file-tools
 
subject: Line Feeds Be Gone