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