Here is a C++ version ... also includes trim functions for right trim, left trim ... or trim both sides ...
// void_string_strip_split.cpp
// this version 2010-05-10
// http://developers-heaven.net/forum/index.php/topic,46.0.html
#include <iostream>
#include <string>
#include <list>
using namespace std;
#define trim strip
#define ltrim lstrip
#define rtrim rstrip
// trim leading and trailing whitespaces from 's' ... and return by 'ref.'
void strip( string& s, const string t = " \t" ) // default whitespace: "\t "
{
size_t p1 = s.find_first_not_of( t ); // get index of 'first char' ...
if( string::npos != p1 ) // ok ... not all ws or empty ... so can safely
{
s.erase( 0, p1);
size_t p2 = s.find_last_not_of( t ); // get index of 'last char' ...
s.erase( p2+1 );
}
else // ... all whitespaces or empty
s.clear();
}
// trim trailing whitespaces only ...
void rstrip( string& s, const string t = " \t" )
{
size_t p2 = s.find_last_not_of( t );
if( string::npos != p2 )
s.erase( p2+1 );
else
s.clear();
}
// trim leading whitespaces only ...
void lstrip( string& s, const string t = " \t" )
{
size_t p1 = s.find_first_not_of( t );
if( string::npos != p1 )
s.erase( 0, p1 );
else
s.clear();
}
void split( list<string>& lst, const string& s, const string delimits = " \t" )
{
size_t p1, p2 = 0;
for( ; ; ) // loop forever ... until break
{
p1 = s.find_first_not_of( delimits, p2 ); // Note: p2 is 0 on first loop
if( string::npos == p1 ) break; // i.e. if empty or all delimits
p2 = s.find_first_of( delimits, p1+1 );
if( string::npos != p2 ) // i.e. if still more ... p2 is not past end
lst.push_back( s.substr( p1, p2-p1 ) );
else
{
lst.push_back( s.substr( p1 ) );
break;
}
}
}
int main()
{
string s, t = "\t leading and trailing whitespaces\t ";
s = t;
cout << "NO strip: '" << s <<"'\n";
strip( s );
cout << " strip: '" << s <<"'\n";
s = t;
rstrip( s );
cout << " rstrip: '" << s <<"'\n";
s = t;
lstrip( s );
cout << " lstrip: '" << s <<"'\n";
string t2 = " \t \t \t ";
s = t2;
cout << "NO trim: '" << s <<"'\n";
trim( s );
cout << " trim: '" << s <<"'\n";
s = t2;
rtrim( s );
cout << " rtrim: '" << s <<"'\n";
s = t2;
ltrim( s );
cout << " ltrim: '" << s <<"'\n";
list < string > myList;
split( myList, t );
cout << "\n\nmyList.size() = "<< myList.size() << endl;
list < string > :: const_iterator it;
int i = 0;
for( it = myList.begin(); it != myList.end(); ++ it, ++ i)
cout << i << ": " << *it << endl;
cout << "\nPress 'Enter' to continue ... " << flush;
cin.get();
}
A version of the above that returns the new list of strings ...
// string_strip_split.cpp
// this version 2010-05-10
// http://developers-heaven.net/forum/index.php/topic,46.0.html
#include <iostream>
#include <string>
#include <list>
using namespace std;
#define trim strip
#define ltrim lstrip
#define rtrim rstrip
// trim leading and trailing whitespaces from returned copy of 's'...
string strip( const string& s, const string t = " \t" )
{
size_t p1 = s.find_first_not_of( t );
if( string::npos != p1 ) // ok ... not all ws or empty ... so can safely
{
size_t p2 = s.find_last_not_of( t ); // get index of 'last char' ...
return s.substr( p1, p2+1-p1 );
}
// else ... all whitespaces or empty ... so return an empty string
return "";
}
// trim leading whitespaces only ...
string lstrip( const string& s, const string t = " \t" )
{
size_t p1 = s.find_first_not_of( t );
if( string::npos != p1 )
return s.substr( p1 );
// else ...
return "";
}
// trim trailing whitespaces only ...
string rstrip( const string& s, const string t = " \t" )
{
size_t p2 = s.find_last_not_of( t );
if( string::npos != p2 )
return s.substr( 0, p2+1 );
// else ...
return "";
}
list < string > split( const string& s, const string delimits = " \t" )
{
list < string > tmp;
size_t p1, p2 = 0;
for( ; ; ) // loop forever ... until break
{
p1 = s.find_first_not_of( delimits, p2 ); // Note: p2 is 0 on first loop
if( string::npos == p1 ) break; // i.e. if empty or all delimits
p2 = s.find_first_of( delimits, p1+1 );
if( string::npos != p2 ) // i.e. if still more ... p2 is not past end
tmp.push_back( s.substr( p1, p2-p1 ) );
else
{
tmp.push_back( s.substr( p1 ) );
break;
}
}
return tmp;
}
int main()
{
string s = "\t leading and trailing whitespaces\t ";
string t = s;
cout << "NO strip: '" << s <<"'\n"
<< " strip: '" << strip( s ) <<"'\n"
<< " rstrip: '" << rstrip( s ) <<"'\n"
<< " lstrip: '" << lstrip( s ) <<"'\n";
s = " \t \t \t ";
cout << "NO strip: '" << s <<"'\n"
<< " strip: '" << trim( s ) <<"'\n"
<< " rstrip: '" << rtrim( s ) <<"'\n"
<< " lstrip: '" << ltrim( s ) <<"'\n";
list < string > mylist = split( t ); // testing split with default delimiters
cout << "\n\nmylist.size() = "<< mylist.size() << endl;
list <string > :: const_iterator it;
int i = 0;
for( it = mylist.begin(); it != mylist.end(); ++ it, ++ i)
cout << i << ": " << *it << endl;
cout << "\nPress 'Enter' to continue ... " << flush;
cin.get();
}