Click to See Complete Forum and Search --> : Trimming spaces in strings
Kohinoor24
November 29th, 2002, 07:16 AM
Hi,
I have a string object,I want to trim spaces from the left & right of the string.Is there any Inbuilt functions for this.The function should be operating system independent.
Thanks in advance...
[Yves : Your title was the beginning of a self-descriptive, good title, so please write it out in full ;) ]
PaulWendt
November 29th, 2002, 08:50 AM
As far as I know there isn't. Here's what I use, though:
string util::trim(const string& s, const string& toTrim /*= " \t\n"*/)
{
return (string(rtrim(ltrim(s, toTrim), toTrim)));
}
string util::ltrim(const string& s, const string& toTrim /*= " \t\n"*/)
{
if (s.length() == 0)
{
return (s);
}
string::size_type index = s.find_first_not_of(toTrim);
if (index == string::npos)
{
return ("");
}
return (string(s, index, s.end() - s.begin()));
}
string util::rtrim(const string& s, const string& toTrim /*= " \t\n"*/)
{
if (s.length() == 0)
{
return (s);
}
string::size_type index = s.find_last_not_of(toTrim);
return (string(s, 0, index+1));
}
There are plenty of algorithms that could accomplish what you
want, though, I'm sure. It might look a little complex though?
With the interface [and code appearance] my boss wanted,
though, these functions work for me.
Edit: Oops, I just noticed these are in a namespace. Just take
off the util:: to make them global functions or make them static
functions of some class or put them in a namespace of your own.
The whole point of this edit is that it probably won't compile
out-of-the-box with the util:: on there :)
--Paul
AlionSolutions
November 29th, 2002, 08:58 AM
Hi,
ok I know no builtin function that does the whole task, but basic_string / string offers a set of functions to do this easy AND compatible. you can port this to any ANSI-C++-environment . I will describe this not with a code sample, because I'm too tired now and don't want t post something with a wrong sytax etc. :p
1. get an iterator for your string
2. iterate throgh the string characters.
3 test each character for whitespace / blank
4. if it is a space-character call the remove() function of basic_string on the iterator.
DONE !!!
the remove()-function removes the character the iterator is pointing at and collapses the following characters to really remove the character.
I hope you got the message, even though my english is terrible when I'm tired ;)
code on
Juergen
PaulWendt
November 29th, 2002, 01:31 PM
He's kind of looking for something that trims off of the ends, not
from the entire string.
--Paul
AlionSolutions
November 29th, 2002, 02:07 PM
hi paul,
hmm my solution is easyly changeable to do this too:
iterator to start of string -> do the things I described until you find something other than whitespace.
iterator to end of string -> iterate backwards until you find something other than whitespace
right ? :D
Juergen
PaulWendt
November 29th, 2002, 10:31 PM
Yes, but it wasn't accurate the way it was typed.
AlionSolutions
November 30th, 2002, 11:48 AM
you are absolutely right... my fault ;)
PaulWendt
November 30th, 2002, 07:21 PM
Hey there's no need for apologies, man. There's often more than
one way to cook a duck or so the saying goes ... or is that skin a
cat? Either way, the point it's good to see multiple ways to attack
the same problem.
--Paul
AlionSolutions
December 1st, 2002, 08:50 AM
Hi Paul,
that's what we are here for: Sharing solutions to avoid the trouble of inventing the wheel each time we want to use one. This gives us more brainspace for "exploring new territories".
If the spirit of this forum would be in the whole world, in each community, I think we would have much less trouble in the world.
keep the spirit
Juergen
Kohinoor24
December 2nd, 2002, 06:33 AM
Thanks..
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.