Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > trim string.

Reply
Thread Tools

trim string.

 
 
DrBob
Guest
Posts: n/a
 
      11-26-2003
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.)
 
Reply With Quote
 
 
 
 
Unforgiven
Guest
Posts: n/a
 
      11-26-2003
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."


 
Reply With Quote
 
 
 
 
Jonathan
Guest
Posts: n/a
 
      11-26-2003

"DrBob" <> wrote in message
news: 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


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      11-27-2003
"Jonathan" <> wrote in message
news:
> "DrBob" <> wrote in message
> news: 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)
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      11-27-2003
"John Carson" <> wrote in message
news:3fc54c80$

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

 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      11-27-2003
In article <3fc55b66$>,
John Carson <> 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 <> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-27-2003
"Jon Bell" <> wrote in message
news:bq416g$jb5$...
| In article <3fc55b66$>,
| John Carson <> 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



 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-27-2003
"DrBob" <> wrote in message
news: 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


 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-27-2003
"Ivan Vecerina" <> wrote in message
news:bq4rtv$nnf$...
| | 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 2 04-24-2007 01:59 AM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 26 02-26-2007 05:06 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 6 12-25-2006 08:47 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 10-25-2006 11:00 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 08-28-2006 11:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57