VC里面如何拆分含汉字与字母的字符串

来源:互联网 发布:cd转mp3软件 编辑:程序博客网 时间:2024/04/20 09:05

给别人的程序打补丁,出现了需要拆分含汉字,字母的字符串的情况,到网上搜到的都是同一段代码

"************* 截取字符串 **************
Function InterceptString(txt,length)
txt=trim(txt)
x = len(txt)
y = 0
if x >= 1 then
for ii = 1 to x
if asc(mid(txt,ii,1)) < 0 or asc(mid(txt,ii,1)) >255 then "如果是汉字
y = y + 2
else
y = y + 1
end if
if y >= length then 
txt = left(trim(txt),ii) "字符串限长
exit for
end if
next
InterceptString = txt
else
InterceptString = ""
end if
End Function 

结果就是测试有些情况下拆分出现乱码,郁闷了好半天,终于发现是网上的这段到处转贴的代码是有错误的,其实这个错误很简单的,就是因为自己没有仔细检查一下就用结果,^_^,这次偶给更正了,希望以后看到的人不会郁闷了.

void CAaaView::OnButton1()
{
 // TODO: Add your control notification handler code here
 CString ChargeItemName;
 CString aa = "9494858受得失测试585858585888d888888888888888";
 int len=0;
 ChargeItemName=InterceptString(len,aa);
 AfxMessageBox(ChargeItemName);               

 len=ChargeItemName.GetLength();
 ChargeItemName=aa.Mid(len); 
 AfxMessageBox(ChargeItemName);
}
CString CAaaView::InterceptString(int qlen, CString strSource)
{
 int len,i,y;
 CString sTemp,sreturn,ceshi;

 strSource.TrimLeft();strSource.TrimRight();
 len=strSource.GetLength();
 y=0;
 sTemp=strSource.Right(len-qlen); 
 
 for(i=0;i<len;i++)
 {
  if(sTemp[y]<0 || sTemp[y]>255)
   y=y+2;
  else
   y=y+1;
  if(y>=26)
   break;
 }
 ceshi.Format("%d",y);
 AfxMessageBox(ceshi);
 sreturn=sTemp.Left(y);

 return sreturn;
}