Trimming basic::string

M

Mike Copeland

What are some simple ways to trim (remove trailing or leading blanks)
a basic::string value? Outside of some clumsy and "brute-force" ways, I
can't seem to find one. TIA
 
M

Mike Copeland

What are some simple ways to trim (remove trailing or leading blanks)
If you're happy using Boost, you could use boost::trim.
Actually, I've never used Boost, and since I'm looking for an answer
soon, it's not a solution. I'd like a way to use standard functions.
Sigh...
 
K

Kai-Uwe Bux

Mike said:
What are some simple ways to trim (remove trailing or leading blanks)
a basic::string value? Outside of some clumsy and "brute-force" ways, I
can't seem to find one. TIA

You can use the method find_last_not_of() to find the last non-blank. Then,
use erase to chop off everything from that point. For someone sufficiently
brave, one could even imagine a one-liner:

str.erase( str.find_last_not_of( " " )+1 );

Try:

std::string a ( "xax " );
std::cout << "|" << a << "|\n";
a.erase( a.find_last_not_of( " " )+1 );
std::cout << "|" << a << "|\n";

However, that is a little tricky: arguing the correctness of the above in
the case that str is empty requires some thought.

The case of trimming a string at the front is left as an exercise :)


Best

Kai-Uwe Bux
 
J

Jonathan Lee

   Actually, I've never used Boost, and since I'm looking for an answer
soon, it's not a solution.  I'd like a way to use standard functions.  
Sigh...

Is there something that string::find_first_not_of(),
string::find_last_not_of() won't handle? Or simply walking the
string with std::isspace() with a locale?

--Jonathan
 
M

Mike Copeland

0Actually, I've never used Boost, and since I'm looking for an answer
Is there something that string::find_first_not_of(),
string::find_last_not_of() won't handle? Or simply walking the
string with std::isspace() with a locale?

No, those are available. The problem is that using a bunch of
functions to determine what to delete and actually do so is convoluted
and difficult to code. I was hoping that there was a simple and clean
routine available somewhere that I could plug into my code - that's what
I'm seeking here.
 
I

Ian Collins

No, those are available. The problem is that using a bunch of
functions to determine what to delete and actually do so is convoluted
and difficult to code. I was hoping that there was a simple and clean
routine available somewhere that I could plug into my code - that's what
I'm seeking here.

There is; in this thread!
 
J

Jonathan Lee

   No, those are available.  The problem is that using a bunch of
functions to determine what to delete and actually do so is convoluted
and difficult to code.  I was hoping that there was a simple and clean
routine available somewhere that I could plug into my code - that's what
I'm seeking here.

See.. here's where I'm confused: as Stuart has basically shown
it's like 5 or 10 lines of code. What difficulties are you
encountering?

--Jonathan
 
M

Mike Copeland

What are some simple ways to trim (remove trailing or leading blanks)
If you were
going to hack something up (without worrying about it necessarily being
too efficient), you could write e.g.

#include <iostream>
#include <string>

std::string trim(const std::string& s)
{
std::string::size_type first = s.find_first_not_of(' ');
if(first == std::string::npos) return "";
std::string::size_type last = s.find_last_not_of(' ');
if(last != std::string::npos) return s.substr(first, last + 1 - first);
else return s.substr(first);
}

int main()
{
std::cout << "'" << trim(" Blah ") << "'\n";
std::cout << "'" << trim(" Wibble") << "'\n";
std::cout << "'" << trim("Ahem ") << "'\n";
std::cout << "'" << trim("Wobble") << "'\n";
std::cout << "'" << trim(" ") << "'\n";
return 0;
}

This is just the sort of thing I was seeking - it's perfect. Thanks
8<}}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top