C + + FAQ: String Split function Split
C + + Standard library There is no character split function split, which can be too inconvenient, I have encountered >3 how to quickly split the string of the problem. Make a few common methods to prepare for a rainy.
Method One: use STL to realize the Split function (common, simple, intuitive)
Prototype: Vector<string> split (const string &s, const string &seperator);
Enter a string, a delimiter string (which can contain multiple delimiters), and return a string vector. This is my favorite method, because it is the most intuitive, in the usual and most commonly used. Implement and test code as follows
#include <vector> #include <string> #include <iostream> using namespace std;
Vector<string> Split (const string &s, const string &seperator) {vector<string> result;
typedef string::size_type String_size;
String_size i = 0;
while (I!= s.size ()) {//Find the first letter in the string that is not equal to the delimiter; int flag = 0;
while (I!= s.size () && flag = 0) {flag = 1;
for (string_size x = 0; x < seperator.size (); ++x) if (s[i] = = Seperator[x]) {++i;
Flag = 0;
Break
}//Find another delimiter, remove the string between the two delimiters; flag = 0;
String_size j = i; while (J!= s.size () && flag = 0) {for (string_size x = 0; x < seperator.size (); ++x) if (s[j) = = Sep
Erator[x]) {flag = 1;
Break
} if (flag = 0) ++j;
} if (I!= j) {Result.push_back (S.substr (i, j-i));
i = j;
} return result;
int main () {string s = ' a,b*c*d,e '; Vector<string&gT v = Split (S, ", *");
Can be separated by several characters;
for (Vector<string>::size_type i = 0; I!= v.size (); ++i) cout << v[i] << "";
cout << Endl; Output: a b c D}
@egmkang provides a simpler and more efficient code that implements the following:
void splitstring (const std::string& S, std::vector<std::string>& V, const std::string& c)
{
Std::string::size_type pos1, Pos2;
Pos2 = S.find (c);
POS1 = 0;
while (Std::string::npos!= pos2)
{
v.push_back (s.substr (POS1, POS2-POS1));
POS1 = Pos2 + c.size ();
Pos2 = S.find (c, POS1);
}
if (pos1!= s.length ())
V.push_back (S.substr (POS1));
Method Two: the Strtok function in C language is used to segment
Prototype: Char *strtok (char *str, const char *delim);
The Strtok function is contained in header file <string.h>, which can be handled in this way for character arrays. Of course, you can also convert a character array to a string and then use method one. The test code is as follows
#include <string.h>
#include <stdio.h>
int main () {
char s[] = "A,b*c,d";
",*"; Char *p can be segmented by multiple characters
;
p = strtok (s, Sep);
while (p) {
printf ("%s", p);
p = strtok (NULL, Sep);
}
printf ("\ n");
return 0;
}
Output: a b c D
Method Three: The Boost library contains the split function
Boost Library has a number of ways to achieve split, also contains a split function, can be used directly, very practical and powerful, but have to download the boost library themselves. Use the following code
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
# Include <vector>
using namespace std;
using namespace boost;
void print (vector <string> & V)
{for
(size_t n = 0; n < v.size (); n++)
cout << "\" < < v[n] << "\ n";
cout << Endl;
}
int main ()
{
string s = "a,b, C,, E,f,";
Vector <string> fields;
cout << "Original = \" "<< s <<" \ "n \ nplease";
cout << "Split on \", \ ' only\n ';
Split (Fields, S, is_any_of (","));
print (fields);
cout << "Split on \", \ \ n;
Split (Fields, S, is_any_of (","));
print (fields);
cout << "Split on \", \ "and Elide delimiters\n";
Split (Fields, S, is_any_of (","), token_compress_on);
print (fields);
return 0;
}
The output results are as follows:
Original = "A,b, C,, E,f,"
split on ", ' only
" a "
" B "
" C "" "
E"
"F" ""
Split on ","
"A" "B" "" "C" "" ""
E "
" F "" "
Split on", "and elide delimiters "
a"
"B"
"C"
"E" "
F"
"" "
In C + + There are many ways to implement the Split function, Cplusplus.com has a C + + split topic, detailed comparison and analysis of several implementation methods (see the following figure). The link receives the reference at the end of the article.
#---------------------------------------------------------------------------------#
Reference Documents
"Accelerated C + +" by Andrew Koenig, Barbara E. Moo.