• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line Feeds Be Gone

 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is an easy way of removeing line feeds "\n" from a string.
Thanks,
Warren Bell
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic