CString類常用方法----Left(),Mid(),Right()

CString類常用方法----Left(),Mid(),Right()……

(2012-06-20 13:34:46)
標籤:

it

分類: 妞的MFC界面開發學

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 個字符


voidMakeUpper(); //這個函數可以將CString字符轉化爲一個大寫的字符串。


注:

在函數後面加 const 的意思是:

如果一個類聲明瞭一個常量對象,這個對象只能使用後邊帶 const 這個的方法.

例:

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


The following example demonstrates the use of CString::MakeUpper.

  // example for CString::MakeUpper
  CString s( "abc" );
  s.MakeUpper();
  ASSERT( s == "ABC" );


在一個較大的字符串中查找字符或子字符串

int Find( TCHAR ch ) const;

int Find( LPCTSTR lpszSub ) const;

int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR pstr, int nStart ) const;

返回值

  返回此CString對象中與需要的子字符串或字符匹配的第一個字符的從零開始的索引;如果沒有找到子字符串或字符則返回-1。

參數

  ch 要搜索的單個字符。
  lpszSub 要搜索的子字符串。
  nStart 字符串中開始搜索的字符的索引,如果是0,則是從頭開始搜索。如果nStart不是0,則位於nStart之前的字符不包括在搜索之內。
  pstr 指向要搜索的字符串的指針

/ CString::Find( TCHAR ch )

  CString s( "abcdef" );
  int n = s.Find( 'c' ); // 結果 n = 2
  int f = s.Find( "de" ) ; // 結果 f = 3
發佈了18 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章