Remove newlines from string 
Author Message
 Remove newlines from string

Hi

What is the "right" and effective way to remove all newlines from a stl string?

Thanks.
Martin



Sat, 19 Mar 2005 20:08:18 GMT  
 Remove newlines from string

Quote:

> Hi

> What is the "right" and effective way to remove all newlines from a stl string?

Hi Martin,

I think this is a good way:
// -----8<-----
#include <algorithm>    // remove
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string s("first line \nsecond line \nthird .. \n***\n");
    cout << "before: " << s << endl;
    s.erase( remove( s.begin(), s.end(), '\n' ), s.end() );
    cout << "after : " << s << endl;
    return( 0 );

Quote:
}

// -----8<-----
Greetings
Werner


Sun, 20 Mar 2005 19:33:04 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Learner Question RE: removing newlines

2. How do you get a newline in a textbox and string in C#

3. insert newlines into long string

4. string newline replace

5. length of strings w. newlines in them

6. stripping newlines from strings

7. newlines in string constants

8. building a string with newline characters

9. newline in string

10. Adding a newline to a string

11. Newline in commandline string

12. formatting long strings using sprintf() without including tabs or newlines

 

 
Powered by phpBB® Forum Software