----CString分割字符-------------------------------------

CString  strPPID="192.168.1"
int iFirst = strPPID.Find('.');
CString strSendcmd = strPPID.Left(iFirst+1);//截取iFirst+1后字符串,4@x@y@angle@1$\r\n


int iFirst = Recvmsg.Find('@');

CString strCamBack = Recvmsg.Mid(iFirst+1);//截取iFirst+1后字符串,4@x@y@angle@1$\r\n
int iSecond = strCamBack.Find('@');
CString strCam = strCamBack.Left(iSecond);//截取右侧iSecond个字节,4

CString strPosXBack = strCamBack.Mid(iSecond+1);//x@y@angle@1$\r\n
int iThird = strPosXBack.Find('@');
CString strPosX = strPosXBack.Left(iThird);

CString strPosYBack = strPosXBack.Mid(iThird+1);//y@angle@1$\r\n
int iFourth = strPosYBack.Find('@');
CString strPosY = strPosYBack.Left(iFourth);

第一种:AfxExtractSubString
此全局函数可用于从特定源字符串中提取子字符串。

包含头文件: #include <afxwin.h>

函数原型:

BOOL AFXAPI AfxExtractSubString ( CString& rString, LPCTSTR lpszFullString,
 int iSubString, TCHAR chSep = '\n');
参数说明:

rString   对CString将得到一个单独的子字符串的对象。
lpszFullString  字符串包含字符串的全文提取自。
iSubString   提取的子字符串的从零开始的索引从lpszFullString。
chSep    使用的分隔符分隔子字符串。

用例说明:

//注意:用于分割的只能是字符,不能是字符串。

//用例一
CString str = _T("abc45,78ea,679u,368");
CString strSub;

AfxExtractSubString(strSub, (LPCTSTR)str, 0, ',');  // strSub的内容为abc45
AfxExtractSubString(strSub, (LPCTSTR)str, 3, ',');  // strSub的内容为368


//用例二
CString strOrg=_T(“192.168.0.1”);

CString strGet1="";
CString strOut1="";

int i=0;
while(AfxExtractSubString(strGet1,strOrg,i,'.'))
{        
 strOut1+=strGet1+"\r\n";
 i++;
}
SetDlgItemText(IDC_EDIT1,strOut1);
第二种:_tcstok函数
该函数是可以从一个CString串中,根据提供的分隔符,截取并返回一个一个的Token;

包含头文件:#include <string.h>

函数原型:

char* _tcstok( char* strToken, const char* strDelimit ); 
  
参数说明: 

strToken: 是一个要分析的串;这个串中包含一个或者多个Token或其他字符;
strDelimit: 是分隔符;根据分隔符把strToken中的Token分析出来;

 用例说明:

  //将以空格符为分隔符对str进行分割 
  CString str = _T("192.168.89.125");  
  TCHAR seps[] = _T(".");  
  TCHAR* token = _tcstok( (LPTSTR)(LPCTSTR)str, seps );  
  while( token != NULL )  
  { 
      printf("str=%s  token=%s\n",str,token);         
      token = _tcstok( NULL, seps );  
  } 

/*
  执行结果如下: 
  str=192  token=192 
  str=192  token=168 
  str=192  token=89 
  str=192  token=125
*/
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落寞的雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值