By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

trim string.

P: n/a
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.)
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
DrBob wrote:
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.

--
Unforgiven

"You can't rightfully be a scientist if you mind people thinking
you're a fool."
Jul 22 '05 #2

P: n/a

"DrBob" <bo*******@yahoo.com> wrote in message
news:8d**************************@posting.google.c om...
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
Jul 22 '05 #3

P: n/a
"Jonathan" <no****@aol.com> wrote in message
news:vs************@corp.supernews.com
"DrBob" <bo*******@yahoo.com> wrote in message
news:8d**************************@posting.google.c om...
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


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);
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #4

P: n/a
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au
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(' '));
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #5

P: n/a
In article <3f********@usenet.per.paradox.net.au>,
John Carson <do***********@datafast.net.au> wrote:

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);

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #6

P: n/a
"Jon Bell" <jt*******@presby.edu> wrote in message
news:bq**********@jtbell.presby.edu...
| In article <3f********@usenet.per.paradox.net.au>,
| John Carson <do***********@datafast.net.au> wrote:
| >
| > 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
--
http://ivan.vecerina.com

Jul 22 '05 #7

P: n/a
"DrBob" <bo*******@yahoo.com> wrote in message
news:8d**************************@posting.google.c om...
| 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
--
http://ivan.vecerina.com
Jul 22 '05 #8

P: n/a
"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:bq**********@newshispeed.ch...
| | 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
--
http://ivan.vecerina.com

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.