trim string.

D

DrBob

gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method to
trim the trailing spaces... (Assuming a method doesn't exist that I'm
not aware of.)
 
U

Unforgiven

DrBob said:
gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method to
trim the trailing spaces... (Assuming a method doesn't exist that I'm
not aware of.)

Use find_first_not_of and find_last_not_of to find the positions of the
first and last non-whitespace characters, then use substr to get only the
part of the string you need.

You should've been able to figure that one out yourself really.
 
J

Jonathan

DrBob said:
gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method to
trim the trailing spaces... (Assuming a method doesn't exist that I'm
not aware of.)

I am not sure about extending the class, but if the first part of your
string does not contain spaces, you could always just look up the index of
the first space, and cut the end off.

string x("abcd ");
string y(x.begin, x.find(' '));

jonathan
 
J

John Carson

Jonathan said:
I am not sure about extending the class, but if the first part of your
string does not contain spaces, you could always just look up the
index of the first space, and cut the end off.

string x("abcd ");
string y(x.begin, x.find(' '));

jonathan

And if you want to modify the original string rather than create a new one:

string x("abcd ");
string::size_type st = x.find(' ');
x.erase(st, x.length()-st);
 
J

John Carson

And if you want to modify the original string rather than create a
new one:

string x("abcd ");
string::size_type st = x.find(' ');
x.erase(st, x.length()-st);

Actually, you only need:

string x("abcd ");
x.erase(x.find(' '));
 
J

Jon Bell

string x("abcd ");
x.erase(x.find(' '));

Of course, that won't work if the string has embedded spaces. How about
searching from the end?

x.erase(x.find_last_not_of(' ')+1);

Or if we want to trim any whitespace (not just blanks):

x.erase(x.find_last_not_of(" \t\n")+1);
 
I

Ivan Vecerina

| In article <[email protected]>,
| >
| > string x("abcd ");
| > x.erase(x.find(' '));
|
| Of course, that won't work if the string has embedded spaces. How about
| searching from the end?
|
| x.erase(x.find_last_not_of(' ')+1);
|
| Or if we want to trim any whitespace (not just blanks):
|
| x.erase(x.find_last_not_of(" \t\n")+1);

Caveat: npos+1 == ? (the npos return value needs to be tested for).
I suspect both of these will fail if no blank is found ...


Regards,
Ivan
 
I

Ivan Vecerina

| I have a string that has trailing spaces in it that I want removed.
| So i have a variable:
| string x("abcd ");
|
| x.trim() isn't an implemented method.
| Is there a method I don't know about?
|
| What do I do to extent the string class such that there is a method to
| trim the trailing spaces... (Assuming a method doesn't exist that I'm
| not aware of.)

No such method => you should implement a non-member function to do so.
I use:


char const kBlankChars[] = " \t\n\r";

/// Returns a string with leading/trailing characters of a set stripped
std::string trimmed
( std::string const& str ///< the original string
, char const* sepSet=kBlankChars ///< chars to be dropped
)
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}

std::string rtrimmed( std::string const& str, char const* sepSet )
{
std::string::size_type const last = str.find_last_not_of(sepSet);
return ( last==std::string::npos )
? std::string()
: str.substr(0, last+1);
}

std::string ltrimmed( std::string const& str, char const* sepSet )
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr( first );
}


Let me know if you see any bug in this...


Ivan
 
I

Ivan Vecerina

| | Or if we want to trim any whitespace (not just blanks):
| |
| | x.erase(x.find_last_not_of(" \t\n")+1);
|
| Caveat: npos+1 == ? (the npos return value needs to be tested for).
| I suspect both of these will fail if no blank is found ...

Woops... no. I sent this too soon.
In fact:
npos is returned if the string only contains blanks.
npos+1 == 0
So it should be ok.

My apologies,
Ivan
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top