CString 截取部分字串的幾種方法

來源:互聯網
上載者:User

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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.