函数void SplitString(const char *aString,char aSeperator)以aSeperator为分隔符aString拆分打印输出,但注意:当aSeperator存在于“引用的字符串中时,或两个aSeperator在一起时,将不作为分隔符,比如假定分隔符是+时,如下为不同情形下的输出示例:
(1)abc+"quoted+quoted"+plus+plus
输出:
abc
"quoted+quoted"
plus+plus
(2)abc+"123++123"+def
输出:
abc
"123++123"
def
(3)abc+"123+test
输出:
abc
"123+test
编程实现函数SplitString
#include <iostream>
using namespace std;
void SplitString(const char *aString, char aSeperator)
{
if(NULL == aString)
{
return ;
}
int flag = 1;
char *p1 = (char *)aString;
while(*p1 != '\0')
{
if(*p1 == '\"')
{
flag = -flag;
cout << *p1;
p1++;
continue;
}
if(flag < 0)
{
cout << *p1;
p1++;
}
else
{
if(*p1 == aSeperator)
{
char *p2 = ++p1;
if(*p2 == aSeperator)
{
cout << aSeperator;
p1=++p2;
}
else
{
cout <<endl;
p1=p2;
}
}
else
{
cout << *p1;
p1++;
}
}
}
}
void main()
{
char *a = "abc+\"quoted+quoted\"+plus++plus";
SplitString(a,'+');
cout << endl<<endl;
char *b = "abc+\"123++123\"+def";
SplitString(b,'+');
cout << endl<<endl;
char *c = "abc+\"123+test";
SplitString(c,'+');
cout <<endl<<endl;
}
abc
"quoted+quoted"
plus+plus
abc
"123++123"
def
abc
"123+test
请按任意键继续. . .