一、需求:把字符串按照给定拆分符(字符或字符串)拆分开:
例如:
a.字符拆分:
a1: 1001,ziweiyi,abcd (末尾无拆分符) 按照’ ,’拆分开成:1001 ziweiyi abcd
a2: 1001,ziweiyi,abcd, (末尾有拆分符) 按照’ ,’拆分开成:1001 ziweiyi abcd
b.字符串拆分:
b1: 1001brziweiyibrabcd (末尾无拆分符) 按照“br”拆分开成:1001 ziweiyi abcd
b2: 1001brziweiyibrabcdbr (末尾有拆分符) 按照“ br”拆分开成:1001 ziweiyi abcd
二、上代码:这里提供两个可用拆分函数,请根据字喜好选择使用
方法一:
1void SplitString(char* src,constchar* separator, vector<string>& destVector) 2{ 3 4 destVector.clear(); 5if(0==strlen(separator)) 6 { 7 destVector.push_back(string(src)); 8return ; 9 }1011char *first,*second;12char tmp[50];13 first = src;14while(first)15 { 16 memset(tmp,'\0',sizeof(tmp));17 second = strstr(first,separator);18if (NULL==second)19 { 20 strncpy(tmp,first,strlen(src));21 }22else23 { 24 strncpy(tmp,first,second-first);25 }2627if (strlen(tmp)!=0)28 { 29 destVector.push_back(string(tmp));30 }31 first = second +strlen(separator);3233if (NULL==second)34break;35 }3637 }
方法二:
1int SplitString(conststring src, string separator, vector<string>& destVector) 2{ 3string temp, SrcTemp; 4int nPos = 0, nResultCount = 0; 5bool bAddEmpty = false; 6 destVector.clear(); 7 SrcTemp = src; 8 9if(separator.empty())10 { 11 destVector.push_back(SrcTemp);12return1;13 }1415do16 { 17 nPos = SrcTemp.find(separator);1819if(nPos != string::npos)20 { 21if(nPos == 0)22 { 23 SrcTemp = SrcTemp.substr(nPos + separator.length(),24 SrcTemp.length() - nPos - separator.length());25continue;26 }27elseif(nPos > 0)28 { 29 temp = SrcTemp.substr(0, nPos);3031 SrcTemp = SrcTemp.substr(nPos + separator.length(),32 SrcTemp.length() - nPos - separator.length());33if (temp.length() > 0)34 { 35 destVector.push_back(temp);36 nResultCount++;37 }38elseif (bAddEmpty)39 { 40 destVector.push_back(temp);41 nResultCount++;42 }4344 }45 }46else47 { 48if(nResultCount == 0)49 { 50if (SrcTemp.length() > 0)51 { 52 destVector.push_back(SrcTemp);53 nResultCount++;54 }55elseif (bAddEmpty)56 { 57 destVector.push_back(SrcTemp);58 nResultCount++;59 }60 SrcTemp = "";61 }62elseif(nResultCount > 0)63 { 64if (SrcTemp.length() > 0)65 { 66 destVector.push_back(SrcTemp);67 nResultCount++;68 }69elseif (bAddEmpty)70 { 71 destVector.push_back(SrcTemp);72 nResultCount++;73 }74 SrcTemp = "";75 }76else77return -1;78 }79 } while(!SrcTemp.empty());8081return nResultCount;82 }
三、测试main
1void main() 2{ 3 vector<string> vecTar; 4char szSrc[]="1001,ziweiyi,abcd"; 5 SplitString(szSrc, ",", vecTar);//方法一 6for (int i = 0; i < vecTar.size();i ++) 7 { 8 printf("【%s】 SplitString: %s\n",szSrc,vecTar[i].c_str()); 9 }10 printf("方法1,end.\n\n");1112string strSrc="1001,ziweiyi,abcd";13int nRet = SplitString(strSrc, ",", vecTar);//方法二14for (int i = 0; i < nRet;i ++)15 { 16 printf("【%s】 SplitString: %s\n",strSrc.c_str(),vecTar[i].c_str());17 }18 printf("方法2,end.\n\n");1920 }
这里指给出了最常见的案例(需求a1的情况),其他的可以自己测试,本人已测试过。
贴出测试效果,更明白:
a1: 1001,ziweiyi,abcd (末尾无拆分符) 按照’ ,‘拆分开成:1001 ziweiyi abcd
a2: 1001,ziweiyi,abcd, (末尾有拆分符) 按照’ ,‘拆分开成:1001 ziweiyi abcd
b.字符串拆分:
b1: 1001brziweiyibrabcd (末尾无拆分符) 按照“br”拆分开成:1001 ziweiyi abcd
b2: 1001brziweiyibrabcdbr (末尾有拆分符) 按照“ br”拆分开成:1001 ziweiyi abcd
转载于:https://blog.51cto.com/leecw/1253459
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/110074.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...