CString 截取部分字符串的几种方法

您所在的位置:网站首页 cstring清空字符串 CString 截取部分字符串的几种方法

CString 截取部分字符串的几种方法

2024-07-11 02:35| 来源: 网络整理| 查看: 265

m_csFileName = csStr.Right(csStr.GetLength()-csStr.ReverseFind('\\')-1); 文件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=2                           OK                         $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);

字符串:D:\cr14\PE\MFC_PE\Debug\MFC_PE.exe

截取目标:MFC_PE.exe

方法:m_csFileName = csStr.Right(csStr.GetLength()-csStr.ReverseFind('\\')-1);



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3