概述:CString是MFC中提供的用於處理字符串的類,是一種很有用的數據類型。
它很大程度上簡化了MFC中的許多操作,使得MFC在做字符串操作時方便了很多。
不管怎樣,使用CString有很多的特殊技巧,特別對於純C背景下走出來的程序員來說有點難以學習。
一、前言
CString位於頭文件afx.h中,這篇文章就來討論這些技巧。
參考百度百科並截取部分對我有用的內容記錄在這篇隨筆中,這篇文章包括以下內容:
<1>CString對象的連接
<2>格式化字符串(包括int轉化為CString)
<3>CString類的成員函數
<4>CString轉化為int型
<5>CString和char*的相互轉化
char*轉化為CString
CString轉化為char*之一:使用LPCTSTR
CString轉化為char*之二:使用CString對象的GetBuffer方法
CString轉化為char*之三:和控件的接口
二、對象連接
CString類重載了+運算符,因此CString對象能對直接使用+號進行連接。而不再使用strcat函數
CString s1, s2, s3; s1 = _T("Hello"); s2 = _T("World"); s3 = s1 + _T(",") + s2 +_T("!"); MessageBox(s3);
注意:MFC編程中,為了兼容Unicode編碼,字符串盡量都使用_T()宏
三、格式化字符串
可以利用CString的Format方法使用C風格字符串格式化一個CString對象,而不再使用sprintf函數
CString str; int dollar = 100; str.Format(_T("I have %d dollars\n"), dollar);
四、成員函數
1.構造函數
CString有很多構造函數,下面只介紹幾個常用的:
(注意函數原型的變量名都是我自己起的,源碼中名字可能不是這個名字,不過不影響)
(另一個類對象引用名采用other, 字符個數采用n)
<1>用已存在的 CString對象 初始化CString對象
函數原型: CString(const CString& other);
<2>可以使用常量字符串初始化CString對象
函數原型: CString(const LPCTSTR other); //這個原型是我猜的
實例: CString str(_T("Hello,World!"));
<3>將字符串other中的前n個字符拷貝到該CString對象
函數原型: CString(LPCTSTR other,int n);
實例: CString str(_T("Hello,World! this is redundant\n"), 12);
<4>用n個字母ch初始化CString對象
函數原型:CString(TCHAR ch,int n = 1);
實例: CString str('6', 6); //str初始化為6個6,即666666
//構造函數 CString s1 = _T("Hello,World!"); //用字符串初始化CString類對象 CString s2 = s1; //用CString類對象初始化CString類對象 CString s3(s1, 5); //用s1的前5個字符初始化類對象 CString s4(_T("Hello,World!"), 5); //用字符串的前n個字符初始化類對象 CString s5(_T('6'), 5); //用n個給定的字符初始化類對象 MessageBox(s1); //輸出Hello,World! MessageBox(s2); //輸出Hello,World! MessageBox(s3); //輸出Hello MessageBox(s4); //輸出Hello MessageBox(s5); //輸出66666
注意:在初始化時使用=運算符調用的是相應類型的構造函數,而不是重載的=運算符,
此外,部分構造函數無法寫成=的形式,比如使用n個ch初始化CString對象,使用字符串的前n個字符初始化類對象等
2.CString類的大小寫轉換及順序轉換函數
<1> 將字符串中的所有大寫字符轉換為小寫字符
函數原型:CString& MakeLower();
<2>將字符串中的所有小寫字符轉換為大寫字符
函數原型:CString& MakeUpper();
<3>將字符串中所有字符的順序顛倒
函數原型:CString& MakeReverse()
<4>要做相應操作,則使用類對象調用相應函數,那么該對象的值便被修改
//CString類的大小寫轉換及順序轉換函數 CString s1 = _T("aaBBCCddEe"); s1.MakeLower(); MessageBox(s1); //輸出 aabbccddee s1.MakeUpper(); MessageBox(s1); //輸出 AABBCCDDEE MessageBox(s1.MakeLower().MakeReverse()); //輸出eeddccbbaa MessageBox(s1); //輸出eeddccbbaa
3.CString對象的連接
多個CString對象的連接可以通過重載運算符+、+=實現。
詳細參見上面的內容(二、對象連接)
//CString對象的連接 CString s1 = _T("Hello"); CString s2 = _T("World!"); MessageBox(s1 + _T(",") + s2 + _T("!")); //輸出Hello,World! s1 += _T(","); s1 += s2; s1 += _T("!"); //s1內容變為Hello,World! MessageBox(s1); //輸出Hello,World!
4.CString對象的比較
CString對象的比較可以通過==、!=、<;、>;、<=、>=等重載運算符實現,也可以使用Compare和CompareNoCase成員函數實現。
<1>==,!=,<,>,<=,>=都是根據ASCII值大小(字符串的字典序)進行比較,
返回值為0或1,1表示使用的比較運算符判斷成立。
<2>Compare函數類似strcmp函數,相等返回0,
小於傳進來的參數則返回小於0的數,大於傳進來的參數則返回大於0的數
<3>CompareNoCase類似Compare,只是不區分大小寫。
//CString對象的比較 CString s1 = _T("aabbcc"); CString s2 = _T("aabbdd"); CString s3 = _T("AAbbcc"); CString s4 = _T("aabbcc"); //部分運算符用法 int a1 = (s1 == s2); //s1 != s2,a1為0 int a2 = (s1 != s2); //s1 != s2,a2為1 int a3 = (s1 <= s3); //s1 > s3, a3為0, 注意大寫字母的ASCII碼比較小 CString str1, str2, str3; str1.Format(_T("%d"), a1); str2.Format(_T("%d"), a2); str3.Format(_T("%d"), a3); MessageBox(str1); MessageBox(str2); MessageBox(str3); //Compare用法 int a4 = s1.Compare(s2); //s1 < s2,a4為-1 int a5 = s1.Compare(s3); //s1 > s2,a5為1 int a6 = s1.Compare(s4); //s1 = s2,a6為0 CString str4,str5,str6; str4.Format(_T("%d"), a4); str5.Format(_T("%d"), a5); str6.Format(_T("%d"), a6); MessageBox(str4); MessageBox(str5); MessageBox(str6); //CompareNoCase用法 int a7 = s1.CompareNoCase(s2); //不區分大小寫, s1 < s2, a7 = -1 int a8 = s1.CompareNoCase(s3); //不區分大小寫, s1 = s2, a8 = 0 int a9 = s1.CompareNoCase(s4); //不區分大小寫, s1 = s2, a9 = 0 CString str7, str8, str9; str7.Format(_T("%d"), a7); str8.Format(_T("%d"), a8); str9.Format(_T("%d"), a9); MessageBox(str7); MessageBox(str8); MessageBox(str9);
5.CString對象字符串的提取操作
<1>提取該字符串左邊nCount個字符的子字符串,並返回一個包含這個子字符串的拷貝的CString對象
函數原型:CString Left(int nCount) const;
<2>提取該字符串右邊nCount個字符的子字符串,並返回一個包含這個子字符串的拷貝的CString對象
//CString對象字符串的提取操作 CString s1 = _T("aabbccddee"); MessageBox(s1.Left(4)); //左邊四個字符,輸出aabb MessageBox(s1.Right(4)); //右邊4個字符,輸出ddee MessageBox(s1.Mid(4, 2)); //輸出從第4個位置開始的兩個字符,即輸出cc MessageBox(s1.Mid(4)); //輸出從第4個位置開始到結尾的子串,即輸出ccddee
6.CString對象字符串的查找操作
//CString對象字符串的查找操作 CString s1 = _T("aabbccbbaa"); //查找子串 int p1 = s1.Find(_T("aa")); //第二個參數默認為0, 故p1為0, int p2 = s1.Find(_T("aa"), 1); //從下表為1開始往后找子串aa, 故p2為8 int p3 = s1.Find(_T("abc")); //未找到,返回-1,即-1 CString str1, str2, str3; str1.Format(_T("%d"), p1); str2.Format(_T("%d"), p2); str3.Format(_T("%d"), p3); MessageBox(str1); MessageBox(str2); MessageBox(str3); //查找字符 int p4 = s1.Find(_T('b')); //第二個參數默認為0,p4為2 int p5 = s1.Find(_T('b'), 4); //從標為4的位置開始往后找,p5為6 int p6 = s1.Find(_T('c'), 6); //未找到,p6為-1 CString str4, str5, str6; str4.Format(_T("%d"), p4); str5.Format(_T("%d"), p5); str6.Format(_T("%d"), p6); MessageBox(str4); MessageBox(str5); MessageBox(str6);
//使用Replace替換子串 CString s1 = _T("aabbccddaabbccdd"); int cnt = s1.Replace(_T("aa"), _T("##")); CString s2; s2.Format(_T("%d"), cnt); MessageBox(s1); //輸出##bbccdd##bbccdd MessageBox(s2); //輸出2
//使用Replace替換字符 CString s1 = _T("aabbccddaabbccdd"); int cnt = s1.Replace('a', '#'); CString s2; s2.Format(_T("%d"), cnt); MessageBox(s1); //輸出##bbccdd##bbccdd MessageBox(s2); //輸出4
//使用Delete刪除字符 CString s1 = _T("aabbccdd"); int len = s1.Delete(2, 2); //刪除bb,len應為6 CString s2; s2.Format(_T("%d"), len); MessageBox(s1); //輸出aaccdd MessageBox(s2); //輸出6
//使用Remove刪除字符 CString s1 = _T("aabbccdd"); int len = s1.Remove(_T('b')); //len為刪除字符個數,應該為2 CString s2; s2.Format(_T("%d"), len); MessageBox(s1); //輸出aaccdd MessageBox(s2); //輸出2
//CString類格式化字符串的方法 CString s1, s2; int cnt = 100; const double PI = 3.141592653; s1.Format(_T("I have %d dollars!"), cnt); s2.Format(_T("PI is approximate to %.2f!"), PI); MessageBox(s1); //輸出I have 100 dollars! MessageBox(s2); //輸出PI is approximate to 3.14!
CString s1 = _T(", World!"); s1.Insert(0, _T("Hello")); //s1為"Hello, World!" MessageBox(s1); CString s2 = _T("ello, World!"); s2.Insert(0, _T('H')); //s2為"Hello, World!" MessageBox(s2);
CString s1 = _T("123"); int n1 = _ttoi(s1); // unsigned long long n2 = _tcstoul(s1, 0, 10); //結果123 long long n3 = _tcstol(s1, 0, 8); //結果為83,八進制123的十進制為83 CString str1, str2, str3; str1.Format(_T("%d"), n1); str2.Format(_T("%llu"), n2); str3.Format(_T("%lld"), n3); MessageBox(str1); MessageBox(str2); MessageBox(str3);
//char*和CString CString s1 = _T("Hello,World!"); CString s2; s2.Format(_T("Hello,World!"));
2.CString轉化為char*
<1>強制類型轉換為 LPCTSTR
A.這是一種略微硬性的轉換,有關"正確"的做法,人們在認識上還存在許多混亂,
正確的使用方法有很多,但錯誤的使用方法可能與正確的使用方法一樣多。
參考:百度百科
