chomp on a string

L

Larry

Hi,

is there any way to get rif of all the \n and \r chars on a
std::string??? I'd like to do the same as chomp by perl does.

thanks
 
T

Thomas J. Gritzan

Am 18.01.2010 02:15, schrieb Larry:
Hi,

is there any way to get rif of all the \n and \r chars on a
std::string??? I'd like to do the same as chomp by perl does.

If you want to remove trailing whitespace (as chomp does), you could use
the member function find_last_not_of and erase of std::string.

Read this and scroll down to the example section:
http://cplusplus.com/reference/string/string/find_last_not_of/

If you want to remove all \n and \r characters, use std::remove and
std::string::erase.
 
L

Larry

If you want to remove all \n and \r characters, use std::remove and
std::string::erase.

Ok, do you think the following could be fine?

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

void chomp(string&);

int main()
{
string s = "My name is Larry \n\r";
chomp(s);
cout << s << endl;

system("pause");
return EXIT_SUCCESS;
}

void chomp(string& szString)
{
string whitespaces(" \t\f\v\n\r");
size_t found;

found=szString.find_last_not_of(whitespaces);
if (found!=string::npos)
szString.erase(found+1);
else
szString.clear();
}

thanks!
 
R

Richard

[Please do not mail me a copy of your followup]

"Larry" <[email protected]> spake the secret code
Ok, do you think the following could be fine?

chomp in perl only removes *trailing* whitespace. Your code removes
everything after the first whitespace character.
 
T

Thomas J. Gritzan

Am 18.01.2010 16:10, schrieb Richard:
"Larry" <[email protected]> spake the secret code


chomp in perl only removes *trailing* whitespace. Your code removes
everything after the first whitespace character.

No. It uses find_last_not_of(whitespaces) to find the last character
that is not whitespace and removes the rest.
 
R

Richard

[Please do not mail me a copy of your followup]

"Thomas J. Gritzan" <[email protected]> spake the secret code
Am 18.01.2010 16:10, schrieb Richard:

No. It uses find_last_not_of(whitespaces) to find the last character
that is not whitespace and removes the rest.

Yes, I noticed that after I hit send :).

s/first/last/

Still, that's not what chomp does. Chomp removes trailing whitespace,
not everything after the last whitespace. A proper implementation of
chomp would be:

#include <iostream>
#include <string>
#include <cassert>

using namespace std;

string chomp(string const &text)
{
string result = text;
string const whitespaces(" \t\f\v\n\r");
size_t const found = result.find_last_not_of(whitespaces);
if (result.length() > 0)
{
while (whitespaces.find(result[result.length() - 1]) != string::npos)
{
result.erase(result.length() - 1);
}
}
return result;
}

int main()
{
string const input("My name is Larry \n\rother stuff\r\n");
string const expected("My name is Larry \n\rother stuff");
assert(expected == chomp(input));
cout << chomp(input);
return 0;
}
 
J

James Kanze

Am 18.01.2010 02:15, schrieb Larry:
If you want to remove trailing whitespace (as chomp does), you
could use the member function find_last_not_of and erase of
std::string.
If you want to remove all \n and \r characters, use
std::remove and std::string::erase.

More generally, you don't need any of the special functions of
std::string. std::find_if with reverse iterators does the job
quite nicely.
 
R

Richard

[Please do not mail me a copy of your followup]

"Larry" <[email protected]> spake the secret code
Ok, do you think the following could be fine?

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

void chomp(string&);

int main()
{
string s = "My name is Larry \n\r";
chomp(s);
cout << s << endl;

system("pause");
return EXIT_SUCCESS;
}

void chomp(string& szString)
{
string whitespaces(" \t\f\v\n\r");
size_t found;

found=szString.find_last_not_of(whitespaces);
if (found!=string::npos)
szString.erase(found+1);
else
szString.clear();
}

My apologies, I misread the "find_last_not_of" as "find the last
whitespace".

Still, I don't know why you call szString.clear() if the result is
npos.

That still seems incorrect, but I'm already 0 for 1 :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Chomp 4
Measuring a string of text 1
chomp? 0
utf8 and chomp 13
Copy string from 2D array to a 1D array in C 1
Trouble accessing a value within a JSON string. 1
string split 4
Problem Splitting Text String 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top