There is a very useful function in Python called strip(). Any similar ones in C++?
Asked
Active
Viewed 2.0k times
14
-
http://stackoverflow.com/questions/352055/best-algorithm-to-strip-leading-and-trailing-spaces-in-c – Veger Feb 20 '12 at 09:28
-
possible duplicate of [What's the best way to trim std::string](http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) – David Heffernan Feb 20 '12 at 09:36
-
possible duplicate of [Python strip a string](http://stackoverflow.com/questions/1038824/python-strip-a-string) – Marcin Feb 20 '12 at 09:36
-
possible duplicate of [Mimic Python's strip() function in C](http://stackoverflow.com/questions/1488372/mimic-pythons-strip-function-in-c) – Lazik Jul 07 '15 at 19:00
-
[`boost::trim`](https://www.boost.org/doc/libs/1_43_0/doc/html/boost/algorithm/trim.html) – sp2danny Sep 09 '21 at 09:37
4 Answers
8
I use this:
#include <string>
#include <cctype>
std::string strip(const std::string &inpt)
{
auto start_it = inpt.begin();
auto end_it = inpt.rbegin();
while (std::isspace(*start_it))
++start_it;
while (std::isspace(*end_it))
++end_it;
return std::string(start_it, end_it.base());
}

Ferdi Kedef
- 126
- 1
- 3
7
There's nothing built-in; I used to use something like the following:
template <std::ctype_base::mask mask>
class IsNot
{
std::locale myLocale; // To ensure lifetime of facet...
std::ctype<char> const* myCType;
public:
IsNot( std::locale const& l = std::locale() )
: myLocale( l )
, myCType( &std::use_facet<std::ctype<char> >( l ) )
{
}
bool operator()( char ch ) const
{
return ! myCType->is( mask, ch );
}
};
typedef IsNot<std::ctype_base::space> IsNotSpace;
std::string
trim( std::string const& original )
{
std::string::const_iterator right = std::find_if( original.rbegin(), original.rend(), IsNotSpace() ).base();
std::string::const_iterator left = std::find_if(original.begin(), right, IsNotSpace() );
return std::string( left, right );
}
which works pretty well. (I now have a significantly more complex version which handles UTF-8 correctly.)

James Kanze
- 150,581
- 18
- 184
- 329
1
void strip(std::string &str)
{
if (str.length() != 0)
{
auto w = std::string(" ") ;
auto n = std::string("\n") ;
auto r = std::string("\t") ;
auto t = std::string("\r") ;
auto v = std::string(1 ,str.front());
while((v == w) || (v==t) || (v==r) || (v==n))
{
str.erase(str.begin());
v = std::string(1 ,str.front());
}
v = std::string(1 , str.back());
while((v ==w) || (v==t) || (v==r) || (v==n))
{
str.erase(str.end() - 1 );
v = std::string(1 , str.back());
}
}

Hybr13
- 80
- 2
- 12

Mhadhbi issam
- 197
- 3
- 6
-
1it work with me good , it is not well optimized but it do the job correctly . i made it void to use it in for_each algorithm . – Mhadhbi issam Sep 25 '19 at 02:52
0
This is on top of the answer provided by Ferdi Kedef to make it safer.
void strip(std::string& str)
{
if (str.length() == 0) {
return;
}
auto start_it = str.begin();
auto end_it = str.rbegin();
while (std::isspace(*start_it)) {
++start_it;
if (start_it == str.end()) break;
}
while (std::isspace(*end_it)) {
++end_it;
if (end_it == str.rend()) break;
}
int start_pos = start_it - str.begin();
int end_pos = end_it.base() - str.begin();
str = start_pos <= end_pos ? std::string(start_it, end_it.base()) : "";
}

xycs
- 121
- 1
- 8