CString截取

转载了一些例子。加上自己遇到的例子,目的是熟悉CString的操作

一,文件xxxx.dll去掉后面的.dll
方法1、

char str[] = "xxxx.dll"
char*p;
p=strrchr(str, '.');
*p = 0;


方法2、

CString str="xxxx.dll";
int n = str.ReverseFind('.')
str = str.Left(str.GetLength()-n-1);



例程2:(csdn)

取得一个字符串中第一个 '?'号之前的字符
方法1

CString m_char,m_disp;
m_disp="jadfueiuajdf?";
m_char="?";
if (!m_char.IsEmpty())
   {
       int index = m_disp.Find(m_char);
       m_disp = m_disp.Right(m_disp.GetLength()-index-1);
   }



返回m_disp就行

方法2

CString temp=the.m_bb;
CString reslut=temp.Left(temp.Find("?")-1);



例程3:(csdn)
一个CString类对象m_StrReceiveModem={ATS0=2OK$03#}
如何截取从$开始的字符串
方法1

 CString m_StrReceiveModem;
       int nPos = m_StrReceiveModem.Find('$');
       if(nPos >= 0)
       {
           CString sSubStr = m_StrReceiveModem.Mid(nPos);//包含$,不想包含时nPos+1
       }

      方法2

CString m_StrReceiveModem;
       int nPos = m_StrReceiveModem.Find('$');
       if(nPos >= 0)
       {
           CString sSubStr = m_StrReceiveModem.Right(StrReceiveModem.GetLength()-nPos); 


       }


 

以及Mid(int nFirst,int nSize)

例程四

定义CString 要找到的一个串截取这个串怎么办???
CString = "abcde base64 baaaaa"
要把base64后面的字符串保留.怎么处理????
现在你给出的字符串已经知道了长度,而且也知道分隔位置在哪儿,直接可以用CString::Right()函数获取后半截,如下:
CString str="abcde base64 baaaaa";
str=str.Right(6);//等式右边得到str的后6个字符组成的字符串然后赋值给str
如果先前不知道分割点的确切位置的话,可以用如下函数查找:
CString::Find() //1
CString::FindOneOf() //2
函数1有如下几个原型:
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;
函数2的原型为:
int FindOneOf( LPCTSTR lpszCharSet ) const;
找到分隔点位置后就可以截取了。
与CString::Right(int n)相对的还有CString::Left(int n),它是用来截取字符串前面n个字符的

CString str="abcde base64 baaaaa";
CString findstr="base64";
CString mystr;
int k=str.Find(findstr)+findstr.GetLength();
mystr= str.Right(str.GetLength()- k);
CString cs;
返回左边的值,cs.left(int x) x为几位;
返回右边的值,cs.right(int x) x为几位;
cs.GetLength();;
我觉得有上面的3个函数,你会使用的话,CString 里面的任意 字符段 都截出来了

AfxMessageBox(mystr);
 
例程五 一个字符串,每遇到换行就保存之前的字符串,并放入数组中。
比如CString strLog=_T("lin1\r\nline2\r\nline3\r\nline4\r\n");
存放数组list<CString> lstStr,lstStr中内容为 line1,line2,line3,line4.
 
CString strLog = _T("lin1\r\nline2\r\nline3\r\nline4\r\n");
	list<CString> lstStrout;
	CString strInfo = "\r\n";
	CString strTemp = strLog;
	
	int index = 0;
	if ( !strTemp.IsEmpty() && (strTemp.Find(strInfo) >= 0) )
	{
		while( 1 )
		{
			index = strTemp.Find(strInfo);

			CString strOut = strTemp.Left(index);
			lstStrout.push_back(strOut);
			if ( ( index + strInfo.GetLength() ) == strTemp.GetLength())
			{
				break;
			}

			strTemp = strTemp.Right(strTemp.GetLength() - index - 2);
		}
	}

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值