Splitting a string by an identifier

Source: Internet
Author: User
Tags strtok

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:

    1. Delimiters are a character set and are used to handle different input formats, such as tab or comma-delimited input
    2. Handle 2 separators between empty cases, 2 requirements, output an empty string or ignore
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.