Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > remove trailing whitespace from string

Reply
Thread Tools

remove trailing whitespace from string

 
 
Donald Canton
Guest
Posts: n/a
 
      02-09-2004
What's the best way to remove trailing whitespace from a string? For
example: "John C. Doe \t" would become "John C. Doe". Thanks.
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-09-2004
Donald Canton wrote:
>
> What's the best way to remove trailing whitespace from a string? For
> example: "John C. Doe \t" would become "John C. Doe". Thanks.


By gooing to www.google.com, switching to the newsgroup archives
and doing some research.
A search string of "C++ trailing whitespace eliminate" comes
up with a lot of good hits.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-09-2004
"Donald Canton" <(E-Mail Removed)> wrote...
> What's the best way to remove trailing whitespace from a string? For
> example: "John C. Doe \t" would become "John C. Doe". Thanks.


Something like

string::reverse_iterator it = somestring.rbegin();
while (it != somestring.rend() && isspace(*it))
{
somestring.erase(it);
it = somestring.rbegin();
}

That's if we're talking of 'string' type. For char array
you do

int lci = strlen(somestring) - 1;
while (lci >= 0 && isspace(somestring[lci])
somestring[lci--] = 0; // move the terminator there

Disclaimer: code wasn't tested.

V


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-09-2004

"Donald Canton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> What's the best way to remove trailing whitespace from a string? For
> example: "John C. Doe \t" would become "John C. Doe". Thanks.


This is untested code

const char* const whitespace = " \t\r\n\v\f";

s.erase(s.find_last_not_of(whitespace) + 1);

john


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-09-2004
Donald Canton wrote:

> What's the best way to remove trailing whitespace from a string? For
> example: "John C. Doe \t" would become "John C. Doe".


I used Google

http://www.google.com/

to search for

+"C++" +"trim string"

and I found lots of stuff.

 
Reply With Quote
 
Tilman Kuepper
Guest
Posts: n/a
 
      02-09-2004
Hello Donald,

> What's the best way to remove trailing whitespace from a string? For
> example: "John C. Doe \t" would become "John C. Doe". Thanks.


If you have to deal with different locales and/or wide-character
strings, the following code might be useful:

template<class E, class T, class A> class C_IsNotSpace_
{
private:
const std::locale& loc;
public:
C_IsNotSpace_(const std::locale& l) : loc(l) { }
inline bool operator()(E ch) const { return !std::isspace(ch, loc); }
};

template<class E, class T, class A>
inline std::basic_string<E, T, A> TrimRight(
const std::basic_string<E, T, A>& str,
const std::locale& loc = std::locale())
{
using namespace std;
typedef basic_string<E, T, A> T_Str;
C_IsNotSpace_<E, T, A> isNotSpace(loc);

typename T_Str::const_reverse_iterator rb = str.rbegin();
typename T_Str::const_reverse_iterator re = str.rend();
typename T_Str::const_reverse_iterator last = find_if(rb, re,
isNotSpace);

return (last == re) ? T_Str() : T_Str(re.base(), last.base());
}

// You can use it like this:

string s1 = " My test string \n";
string s2 = TrimRight(s1);

Tilman


 
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
Regex to match all trailing whitespace _and_ newlines. Dotan Cohen Python 2 10-10-2011 10:23 PM
Remove only TRAILING whitespace Bob Smyph Ruby 4 10-14-2008 06:56 PM
QUERY: strip trailing whitespace ... woes :S KevinGPO C++ 1 02-20-2006 01:48 PM
Q: Regexp for fixed-length string with trailing whitespace andreas1234567@hotmail.com Perl Misc 3 09-09-2005 12:26 PM
Reading words with trailing whitespace Brad Marts C++ 3 11-17-2003 11:31 PM



Advertisments