|
-
November 29th, 2002, 08:16 AM
#1
Trimming of..
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 ]
Last edited by Yves M; November 29th, 2002 at 10:04 AM.
-
November 29th, 2002, 09:50 AM
#2
As far as I know there isn't. Here's what I use, though:
Code:
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
-
November 29th, 2002, 09:58 AM
#3
i know no builtin function but...
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.
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
-
November 29th, 2002, 02:31 PM
#4
He's kind of looking for something that trims off of the ends, not
from the entire string.
--Paul
-
November 29th, 2002, 03:07 PM
#5
ok
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 ?
Juergen
-
November 29th, 2002, 11:31 PM
#6
Yes, but it wasn't accurate the way it was typed.
-
November 30th, 2002, 12:48 PM
#7
yess
you are absolutely right... my fault
-
November 30th, 2002, 08:21 PM
#8
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
-
December 1st, 2002, 09:50 AM
#9
yeppp
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
-
December 2nd, 2002, 07:33 AM
#10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|