473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.)
Jul 22 '05 #1
8 17697
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

"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
"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
"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
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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
13
by: Jonathan Wood | last post by:
According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear to...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
4
by: Oleg Subachev | last post by:
If I apply Trim() method to string consisting of two x0D and x0A characters the resulting string is not empty but contain the same two charaters: x0D, x0A. But according to the help Trim()...
8
by: Kevin Smith | last post by:
Hi, According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear...
8
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.