• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Line Feeds Be Gone

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is an easy way of removeing line feeds "\n" from a string.
Thanks,
Warren Bell
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
reply
    Bookmark Topic Watch Topic
  • New Topic