Both Java and C # can use split to split the string, but in C + + There is no such method, before always write a function of their own segmentation, not trouble, today on the Internet to find a similar function, found that Strtoc () seems to be able to complete the segmentation function of the string.
Prototype: Char *strtok (char s[], const char *delim);
Usage: decomposes a string into a set of strings. S is the character to be decomposed, and the Delim is a delimiter character (in the case of a string, the first is the split criterion). On first call, S points to the string to be decomposed, and then calls to set S to null. I don't quite understand why we should put s into null after that.
Using Strtok to achieve split
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
String str= "ABC def,aaa *BBB";
const char *D = ", *";
Char *p;
p = strtok ((char *) str.c_str (), d);
while (p)
{
cout<<p<<endl;
P=strtok (NULL,D);
}
return 0;
}
It is important to note that strtok is a thread unsafe function because it uses statically allocated space to store the segmented string position.
C + + string segmentation