It is often encountered to split a string based on an identifier and save the result of the split to a string array. The following requirements have been encountered:
- Delimiters are a character set and are used to handle different input formats, such as tab or comma-delimited input
- Handle 2 separators between empty cases, 2 requirements, output an empty string or ignore
- Handle carriage returns at the end of a string, 2 requirements, ignored or deleted
The C language's strtok function supports a split string, which ignores empty elements when it encounters a null content between delimiters, pushes forward to return the next non-empty string, and returns null after the partition is complete. This function seems to have a multi-threaded version.
//The split string, if the result of the split is encountered, is an empty string also as output.
//substr (Begin,offset) returns an empty string when begin is illegal, offset is 0, or too large
voidSplit1 (Const string& str)
{
std::vector<string> splitstr;
Char* separator="\ t,";
string:: Size_type begin=0;
string:: Size_type Nextpos;
Do{
Nextpos = str.find_first_of (Separator,begin);
//even if substr returns an empty string, it also records
Splitstr.push_back (Str.substr (Begin,nextpos-begin));
Begin = Nextpos+1;
} while(Nextpos! =string:: NPOs);
//remove the last line break
if(Splitstr.size () >0){
string& lastele = Splitstr.back ();
if(Lastele.size () >0&& * (Lastele.end ()-1)=='\ n')
{
Lastele.erase (Lastele.end ()-1);
}
}
for(size_t i=0; I < splitstr.size (); ++i)
{
printf"case1 idx=%d, len=%d, s=%s\n", I,splitstr[i].size (), Splitstr[i].c_str ());
}
}
//splits a string, skipping an empty string.
voidSplit2 (string& Strcellcfg)
{
std::vector<string> splitstr;
Charauctmp[ -];
strcpy (Auctmp,strcellcfg.c_str ());
ConstChar* Separator ="\ t,";
//The tok function skips the empty string in the middle of the "a,,b" delimiter, and returns null if the content is not found, such as ","
//so the following implementations do not include an empty string in the resulting result
Char* Ptoke = Strtok (auctmp,separator);
while(Ptoke!=null)
{
Splitstr.push_back (Ptoke);
Ptoke = Strtok (null,separator);
}
if(Splitstr.size () >0){
string& lastele = Splitstr.back ();
if(Lastele.size () >0&& * (Lastele.end ()-1)=='\ n')
{
Lastele.erase (Lastele.end ()-1);
}
}
for(size_t i=0; I < splitstr.size (); ++i)
{
printf"Case2 idx=%d, len=%d, s=%s\n", I,splitstr[i].size (), Splitstr[i].c_str ());
}
}
voidTestsplit ()
{
stringt[]={" the","1 2 3",
"h\n",
"",
",",
",,",
"A,"};
for(intI=0; I <7; ++i)
{
Split1 (T[i]);
Split2 (T[i]);
}
}
Test results:
Case1 idx=0, len=1, s=1
Case1 idx=1, len=1, s=2
Case1 idx=2, len=1, s=3
Case2 idx=0, len=1, s=1
Case2 idx=1, len=1, s=2
Case2 idx=2, len=1, s=3
Case1 idx=0, len=1, s=1
Case1 idx=1, len=1, s=2
Case1 idx=2, len=1, s=3
Case2 idx=0, len=1, s=1
Case2 idx=1, len=1, s=2
Case2 idx=2, len=1, s=3
Case1 idx=0, len=1, s=h
Case2 idx=0, len=1, s=h
Case1 idx=0, len=0, s=
Case1 idx=0, len=0, s=
Case1 idx=1, len=0, s=
Case1 idx=0, len=0, s=
Case1 idx=1, len=0, s=
Case1 idx=2, len=0, s=
Case1 idx=0, len=1, S=a
Case1 idx=1, len=0, s=
Case2 idx=0, len=1, S=a
Splitting a string by an identifier