C++ String split

November 3, 2010 at 09:43:36
Specs: Windows XP
// stringSplit.cpp (c) 2010 adolfo@di-mare.com

/*
THIS IS FREE SOFWTWARE.
- Use at your own risk; acknowledge authorship, please.
- You can never blame the author for your use of this software.
- Released to the world under LGPLv3:
http://www.gnu.org/licenses/lgpl-3....
ESTE PROGRAMA ES LIBRE.
- Uselo baja su propia responsabilidad; reconozca la autorĂ­a, por favor.
- Usted nunca podrĂ¡ culpar al autor por su propio uso de este software.
- Entregado al mundo bajo LGPLv3:
http://www.gnu.org/licenses/lgpl-3....
*/

#include <string>
#include <vector>

/** Split a \c str around \c delim.
Inspired in Java's String.split() method.
Usefull to process CSV files.

\return
The array of strings computed by splitting this string around matches
to \c delim.

\code
const char csv = { "0,1,,333" },
split( csv, ",", STR ); {
assertTrue( STR[0] == "0" );
assertTrue( STR[1] == "1" );
assertTrue( STR[2] == "" );
assertTrue( STR[3] == "333" );
assertTrue( STR.size() == 4 );
}
\endcode
*/
void split( const std::string& str, const std::string& delim, std::vector<std::string>& results );

void split( const std::string& str, const std::string& delim, std::vector<std::string>& results ) {
results.clear();
if ( str.empty() ) { return; }

size_t left, pos=0;
while ( std::string::npos != (left = str.find(delim,pos)) ) {
results.push_back( str.substr(pos,left-pos) );
pos = left+delim.length();
}
if (pos < str.length() ) {
results.push_back( str.substr(pos) );
}
}


/* Test cases follow. Remove when not testing. */

#if 0
#include <cassert> // macro assert()
using namespace std;

#define assertTrue(X) assert(X)

const char* csv[] = {
"Jonny Cash,1,22,333,4444,55555,666666",
"0,1,,333",
"ring,tone,ring tone"
};

int main() {
vector<string> STR;

split( csv[0], ",", STR ); {
assertTrue( STR[0] == "Jonny Cash" );
assertTrue( STR[1] == "1" );
assertTrue( STR[2] == "22" );
assertTrue( STR[3] == "333" );
assertTrue( STR[4] == "4444" );
assertTrue( STR[5] == "55555" );
assertTrue( STR[6] == "666666" );
assertTrue( STR.size() == 1+6 );
}

split( csv[1], ",", STR ); {
assertTrue( STR[0] == "0" );
assertTrue( STR[1] == "1" );
assertTrue( STR[2] == "" );
assertTrue( STR[3] == "333" );
assertTrue( STR.size() == 4 );
}

split( csv[2], ",", STR ); {
assertTrue( STR[0] == "ring" );
assertTrue( STR[1] == "tone" );
assertTrue( STR[2] == "ring tone" );
assertTrue( STR.size() == 3 );
}
return 0;
}
#endif

#if 0
// Code copied from:
// http://www.computing.net/answers/pr...

StringSplit(string str, string delim, vector<string> results)
{
int cutAt;
while( (cutAt = str.find_first_of(delim)) != str.npos )
{
if(cutAt > 0)
{
results.push_back(str.substr(0,cutAt));
}
str = str.substr(cutAt+1);
}
if(str.length() > 0)
{
results.push_back(str);
}
}

#endif

// EOF: stringSplit.cpp



See More: C++ String split

Report •

#1
November 3, 2010 at 11:38:55
Why not have split() return the vector, instead of taking it as an output argument?

Also, if you are using this to split CSV records, beware that they may contain embedded commas that this function doesn't deal with, e.g.

"one,two,\"thr,ee\",four"


Report •
Related Solutions


Ask Question