trim string.



 DEVELOP > c-Plus-Plus > trim string.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "DrBob"
Date: 26 Nov 2003 04:12:33 PM
Object: trim string.
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.)
.

User: "Jonathan"

Title: Re: trim string. 26 Nov 2003 04:25:32 PM
"DrBob" <bobrien18@yahoo.com> wrote in message
news:8d7548cf.0311261412.4bfbf132@posting.google.com...

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
.
User: "John Carson"

Title: Re: trim string. 26 Nov 2003 06:59:32 PM
"Jonathan" <nospam@aol.com> wrote in message
news:vsaa3sg4smu153@corp.supernews.com

"DrBob" <bobrien18@yahoo.com> wrote in message
news:8d7548cf.0311261412.4bfbf132@posting.google.com...

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)
.
User: "John Carson"

Title: Re: trim string. 26 Nov 2003 08:03:14 PM
"John Carson" <donaldquixote@datafast.net.au> wrote in message
news:3fc54c80$1@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)

.
User: "Jon Bell"

Title: Re: trim string. 26 Nov 2003 11:13:53 PM
In article <3fc55b66$1@usenet.per.paradox.net.au>,
John Carson <donaldquixote@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 <jtbellap8@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
.
User: "Ivan Vecerina"

Title: Re: trim string. 27 Nov 2003 06:50:20 AM
"Jon Bell" <jtbellq2f@presby.edu> wrote in message
news:bq416g$jb5$1@jtbell.presby.edu...
| In article <3fc55b66$1@usenet.per.paradox.net.au>,
| John Carson <donaldquixote@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
.
User: "Ivan Vecerina"

Title: Re: trim string. 27 Nov 2003 06:55:21 AM
"Ivan Vecerina" <please_use_web_form@ivan.vecerina.com> wrote in message
news:bq4rtv$nnf$1@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
.






User: "Ivan Vecerina"

Title: Re: trim string. 27 Nov 2003 06:51:58 AM
"DrBob" <bobrien18@yahoo.com> wrote in message
news:8d7548cf.0311261412.4bfbf132@posting.google.com...
| 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
.

User: "Unforgiven"

Title: Re: trim string. 26 Nov 2003 04:22:19 PM
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."
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER