1.Left(), Mid(), Right()
CStringLeft(intnCount)const; //從左邊1開始獲取前 nCount個字符
CStringMid(intnFirst)const; //從左邊第 nFirst+1個字符開始,獲取後面所有的字符
CStringMid(intnFirst,intnCount)const; //從左邊第 nFirst+1 個字符開始,獲取後面nCount個字符
CStringRight(intnCount)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
2.在一個較長的字符串中查找字符或子字符串
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
intFind( TCHAR ch, int nStart ) const;intFind( LPCTSTR pstr, int nStart ) const;
返回值:返回此CString對象中與需要的子字符串或字符匹配的第一個字符的從零開始的索引;如果沒有找到子字符串或字符則返回-1。
參數:
ch要搜索的單個字符。
lpszSub要搜索的子字符串。
nStart字符串中開始搜索的字符的索引,如果是0,則是從頭開始搜索。如果nStart不是0,則從nStart位開始搜索。 pstr指向要搜索的字符串的指針。
範例:
CString::Find( TCHAR ch )
CStrings( "abcdef" );
intn = s.Find( 'c' ); // 結果 n = 2
intf = s.Find( "de" ) ; // 結果 f = 3
3.MakeUpper(), MakeLower(), MakeReverse()
void MakeUpper( ); //使所有小寫字母變爲大寫
void MakeLower( ); //使所有大寫字母變爲小寫
void MakeReverse( ); //倒序字符串
範例:
CString a,b,c;
a = "ABcd";
a.MakeUpper(); //結果爲"ABCD"
a.MakeLower(); //結果爲"abcd"
a.MakeReverse(); //結果爲"dcba"