《VC用字符串截取字符串的函数(比AfxExtractSubString更好)》原出处:
http://blog.youkuaiyun.com/jaketseng/archive/2007/11/25/1901623.aspx
该代码存在一些问题:
例:
Split(_T(
"
,asfd,234sdf,0asdf,s0apf
"
), dest, _T(
"
,
"
));
截取后得到的字符串数组为:
"
,asfd
"
、
"
,234sdf
"
、
"
,0asdf
"
、
""
【最后一个为空字符串,并不是期望的
"
s0apf
"
或
"
,s0apf
"
】
INT_PTR Split_CString(
const
CString
&
source,
//
需要截取的原字符串
CStringArray
&
dest,
//
分割后的字符串数组
const
CString
&
division
//
用做分割符的字符串
)
//
使用方式:Split(strViewString, dest, "<div id="pro_detail">");

...
{
if( source.IsEmpty() )
return -1;
dest.RemoveAll();
int len = division.GetLength();
int iFirst = 0;
int nCount = 0;
int pos = 0;
int pre_pos = -1;
while( -1 != pos )

...{
if( -1 == pre_pos )
pos = source.Find(division,pos);
else
pos = source.Find(division,(pos+1));

if( -1 == pre_pos )

...{
iFirst = 0;
if( -1 == pos )
nCount = source.GetLength();
else
nCount = pos;
}
else

...{
iFirst = pre_pos+len;
if( -1 != pos )
nCount = pos - pre_pos - len;
else
nCount = source.GetLength()-pre_pos-len;
}

dest.Add(source.Mid(iFirst,nCount));

pre_pos = pos;
}

return dest.GetCount();
}
http://blog.youkuaiyun.com/jaketseng/archive/2007/11/25/1901623.aspx
该代码存在一些问题:




修正后的代码如下:
















































测试:
Split(_T("asfd,234sdf,0asdf,s0apf"), dest, _T(","));
截取后得到的字符串数组为:"asfd"、"234sdf"、"0asdf"、"s0apf"
Split(_T(",asfd,234sdf,0asdf,s0apf"), dest, _T(","));
截取后得到的字符串数组为:""、"asfd"、"234sdf"、"0asdf"、"s0apf"
Split(_T(",asfd,234sdf,0asdf,,s0apf"), dest, _T(","));
截取后得到的字符串数组为:""、"asfd"、"234sdf"、"0asdf"、""、"s0apf"