分隔字符串

1.Cstring中的 Left()、Mid()、Right() 用法

CString Left( int nCount ) const; //从左边1开始获取前 nCount 个字符

CString Mid( int nFirst ) const; //从左边第 nCount+1 个字符开始,获取后面所有的字符

CString Mid( int nFirst, int nCount ) const; //从左边第 nFirst+1 个字符开始,获取后面 nCount 个字符

CString Right( int nCount ) const; //从右边1开始获取从右向左前 nCount 个字符

str.Find(",",0);//从第一位开始左往右找到第一个“,”的位置,搭配Left
str.Find(",",1);//从第二位开始左往右找到第一个“,”的位置,搭配Left
str.ReverseFind(',');//从右往左找到第一个“,”的位置,搭配Right

str.Replace("-",",")//“-”用“,”代替

str = str.Trim(_T(" "));//删除str字符串的头尾空白符
ps:TrimEnd()和 TrimStart() 用法和Trim()相似,只是单独去除左侧或右侧空格或字符;

 CString a,b;
 a = "123456789";
 b = a.Left(4);   //值为:1234
 b = a.Mid(3);    //值为:456789
 b = a.Mid(2, 4); //值为:3456
 b = a.Right(4);  //值为:6789

例2
strDate = str.Right(str.GetLength()-str.ReverseFind(',')-1);//找到日期
pos1 = strDate.Find('/');//找到第一个“/的位置”
pos2 = strDate.Find('/', pos1+1); //找到pose+1后的第一个“/”

year = _ttoi(strDate.Left(pos1));
month = _ttoi(strDate.Mid(pos1+1, pos2-pos1-1)); 
day = _ttoi(strDate.Mid(pos2+1, strDate.GetLength() - pos2 - 1));

2.MFC分割 字符串的函数AfxExtractSubString(CString& rString, LPCTSTR lpszFullString,int iSubString, TCHAR chSep = '/n')

        参数说明:

        rString 得到的字符串;lpszFullString 待分割的字符串;iSubString 要得到第几个字符串;chSep 个子串之间的分隔符

        例如,有一个字符串strFullString = "abcd-hgdy-weiuiwu-sdlsk";则有:

CString strTmp;

AfxExtractSubString( strTmp, (LPCTSTR)strFullString, 0, '-');//strTmp的内容为abcd

AfxExtractSubString( strTmp, (LPCTSTR)strFullString, 2, '-');//strTmp的内容为weiuiwu
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值