CString 類的使用心得

未定稿,轉載請指名出處。

 

先定義幾個以後會用到的變量:

CString str1, str2, str3;

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

 

概括說明:

    MFCCString類的封裝可能的確不如std::string完善,但是也的確不錯,功能也足夠強大,使用上還很體貼。其基本特徵爲:

    CString類沒有基類。

    CString類和LPCTSTR的關係:MSDN上說“CString objects follow "value semantics." Think of a CString object as an actual string, not as a pointer to a string.也就是說用的時候除了考慮使用它的成員函數的方式,就把它當作普通的c-style字符串來使用就行了。你可以在構造函數中使用LPCTSTR:

       CString str("Hello World!");

可以:

       str1 = str2;                或者   str1 = “Hello”;

也可以:

       str3 = str1 + str2;     或者   str3 = str1 + “Hello”;

當然也可以:

       str1 += str2;              或者      str1 += “Hello”;

實際上下列的這些操作符都可以用在CString對象之間或者CStringLPCTSTR對象之間:

       ==!=<><=>=

自然,將CString對象強制轉換到LPCTSTR類型也就應該在情理之中:

       LPCTSTR string = (LPCTSTR) str1;

    CString支持UNICODE和多字節字符集(MBCS)。因爲其本身是基於TCHAR的——當然你不要忘了定義編碼方式,如:#define _UNICODE

    CString支持引用計數。可以通過其成員函數LockBuffe/和UnLockBuffer來禁用/啓用引用計數。

 

對於CString類的成員函數的定義、說明、返回值等形式在此並不贅述,如有此疑問請參閱:http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cstring_class_members.asp中的相關鏈接。

 

常用函數和範例:

改變大小寫:

CString::MakeUpperCString::MakeLower兩個成員函數(不帶參數)能使整個字符串變成大/小寫字母。

例:       str1 = “hello”;

               str1.MakeUpper();

               afxDump << str1;         // 輸出結果是”HELLO”

☆反轉:void CString::MakeReverse();

☆從.rc文件讀入字符串:

CString::LoadString函數把傳入的字符串資源ID對應的字符串讀入到CString對象中。如果成功就返回非零值。

       BOOL bResult = LoadString(IDS_FILENOTFOUND);

☆子串操作

→去掉字符串左邊空格:str1.TrimLeft();

→去掉字符串右邊空格:str1.TrimRight();

→獲得指定位置字符:char a = str1.GetAt(3); 相應的有CString::SetAt函數,修改指定位置字符。

→刪除字符串中所有指定字符:

       str1 = “Hello test”;

       str1.Remove(‘t’);

       afxDump << str1;         //輸出”Hello es”

→刪除指定位置指定長度的子串:

       str1 = “Hello test”;

       str1.Delete(3, 2);           //第一個參數爲index(從零開始)

                                          //第二個參數是長度

       afxDump << str1;         //輸出”Hel test”

→清空對象的內容:

void CString::Empty();

→查找子串:

CString::Find函數有四個重載,可以進行字符和字串的搜索,同時還可以指定搜索的起始位置,並返回第一次查找到的index

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::ReverseFind是返回字符串中最後一個匹配字符的index,與Find函數查找方向正好相反,可惜只有一種重載:

int ReverseFind( TCHAR ch ) const;

CString::FindOneof查找的是第一個與指定字符串中任意一個匹配字符的index。(好像有點繞口,看看例子就明白了)

       str1 = “Hello test”;

       int j = str1.Find(“el”);

       afxDump << “j=” << j << “/n”;

       int k = str1.Find(‘e’, 3);

       afxDump << “k=” << k << “/n”;

       int l = str1.ReverseFind(‘t’);

       afxDump << “l=” << l << “/n”;

       int m = str1.ReverseFind(‘t’);

       afxDump << “m=” << m << “/n”;

       int n = str1. FindOneOf(“stuv”);

       afxDump << “n=” << n << “/n”;

輸出結果:

       j=1

       k=7

       l=9

       m=9

       n=6

→字串截斷:CString::LeftCString::Right函數都只帶一個參數,並且都返回一個CString對象,作用是截取左/右邊指定長度的子串。CString::Mid函數第一個參數指定位置,第二個參數指定長度。這幾個都是常用的函數,就不寫例子了

→獲得Buffer

經常有人問到CString對象和char *的轉換問題,除了前面說到的強制轉化,就是用這個了

LPTSTR GetBufferSetLength( int nNewLength );使用返回的指針可以直接修改CString對象的內容,不過有兩點要注意,一是如果指定長度比原CString長度短(截斷)請記得在後面補’/<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0’,二是在調用CString對象的任何其它成員函數前請一定記得ReleaseBuffer,也許不用似乎並沒有出錯,但是說不定就是大隱患的根源。

CString::SpanExcluding函數

以前回答過一個把CString對象分離子串的問題,現在想想,如果當時用這個函數的話,將使多麼的方便。函數原型:

CString SpanExcluding( LPCTSTR lpszCharSet ) const;

它查找CString對象中與lpszCharSet串中任意匹配的第一個字符,並返回一個CString對象,該對象的內容是原來對象從起始位置到查找到字符的前一個字符的部分。這在分離用分割符(逗號空格之類)隔開的子串的時候將十分方便:

       str1 = “Hello test”;

       str2 = str1.SpanExcluding(“ ,”);

       afxDump << str2;         //輸出”Hello”

同時,還有一個很方便的函數:CString::SpanIncluding,函數原型:

CString SpanIncluding( LPCTSTR lpszCharSet ) const;

它返回對象中前若干個字符,這些字符都必須在lpszCharSet之中:

       str1 = “Hello test”;

       str2 = str1.SpanIncluding(“ABCDEFGHIJK”);

       afxDump << str2;         //輸出”H”

→插入子串:用CString::Insert可以插入字符或者字串到指定位置

       str1 = “Hello test”;

       str1.Insert(2,“ABCD”);

       afxDump << str1;         //輸出”HeABCDllo test”

→替換:CString::Replace的作用是將原來對象中的所有匹配相替換指定字符/子串。有兩個重載原型:

int Replace( TCHAR chOld, TCHAR chNew );

int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

CString對象的屬性操作:這些都很常用了,簡要說明之

int GetLength( ) const;          //獲得buffer的長度

BOOL IsEmpty( ) const;              //判斷CString對象內容是否爲空

int Compare( LPCTSTR lpsz ) const;   //lpszASCII碼比較

int CompareNoCase( LPCTSTR lpsz ) const;             //lpszASCII碼比較,忽略大小寫

CString::Format             /*用來格式化對象。切記不要把對象本身放到Format函數的參數中去了*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章