CString* SplitString(CString strOrignal/*原串*/, char cSplit/*分割符*/, int &iSubStrCnt/*子串个数*/)
{
int iPos = 0; //分割符位置
int iNums = 0; //分割符的总数
CString *pStrSplit; //子字符串数组
CString strTemp = _T("");
CString strLeft = _T("");
CString strRight = _T("");
strTemp = strOrignal;
//先计算子字符串的数量
while (iPos != -1)
{
iPos = strTemp.Find(cSplit);
if (iPos == -1)
{
break;
}
strRight = strTemp.Mid(iPos+1, strOrignal.GetLength());
strTemp = strRight;
iNums++;
}
//没有找到分割符
if (iNums == 0)
{
//子字符串数就是字符串本身
iSubStrCnt = 1;
return NULL;
}
//根据子串的个数,动态为子字符串数据分配空间
iSubStrCnt = iNums + 1;
pStrSplit = new CString[iSubStrCnt];
strTemp = strOrignal;
//分割
for (int i=0; i<iNums; i++)
{
iPos = strTemp.Find(cSplit);
//左子串
if (iPos==0)
{
strLeft = "";
}
else
{
strLeft = strTemp.Left(iPos);
}
//右子串
strRight = strTemp.Mid(iPos+1, strTemp.GetLength());
strTemp = strRight;
pStrSplit[i] = strLeft;
}
//存储最右边的一个子字符串
pStrSplit[iNums] = strTemp;
return pStrSplit;
}
字符串分割函数
最新推荐文章于 2024-01-19 18:30:00 发布