①、CString 類對象的初始化:
CString str;
CString str1(_T("abc"));
CString str2 = _T("defg");
TCHAR szBuf[] = _T("kkk");
CString str3(szBuf);
CString str4 = szBuf;
TCHAR *p = _T("1k2");
//TCHAR * 轉換爲 CString
CString str5(p);
CString str6 = p;
CString str7(str1);
CString str8 = str7;
②、字符串基本操作:
● 長度:GetLength();
CString str(_T("abc"));
int len = str.GetLength(); //len == 3
● 是否爲空,即不含字符:IsEmpty();
● 清空字符串:Empty();
CString str(_T("abc"));
BOOL mEmpty = str.IsEmpty(); //mEmpty == FALSE
str.Empty();
mEmpty = str.IsEmpty(); //mEmpty == TRUE
● 轉換大小寫:MakeUpper、MakeLower
● 轉換順序:MakeReverse
CString str(_T("Abc"));
str.MakeUpper(); //str == ABC
str.MakeLower(); //str == abc
str.MakeReverse(); //str == cba
● 字符串的連接:+、+=
CString str(_T("abc"));
str = _T("de") + str + _T("kp"); //str == deabckp
str += _T("123"); //str == deabckp123
TCHAR szBuf[] = _T("789");
str += szBuf; //str == deabckp123789
● 字符串的比較:==、!=、(<、>、<=、>= 不常用)、Compare(區分大小寫)、CompareNoCase(不區分大小寫)
CString str1(_T("abc"));
CString str2 = _T("aBc");
if (str1 == str2){
MessageBox(_T("str1 等於 str2"));
}else{
MessageBox(_T("str1 不等於 str2"));
}
③、字符串的查找:
Find、ReverseFind、FindOneOf 三個函數可以實現字符串的查找操作
Find 從指定位置開始查找指定的字符或者字符串,返回其位置,找不到返回 -1;
舉例:
CString str(_T("abcdefg"));
int idx = str.Find(_T("cde"), 0); //idx 的值爲2;
ReverseFind 從字符串末尾開始查找指定的字符,返回其位置,找不到返回 -1,雖然是從後向前查找,但是位置爲從開始算起;
CString str(_T("abcdefg"));
int idx = str.ReverseFind('e'); //idx 的值爲4;
FindOneOf 查找參數中給定字符串中的任意字符,返回第一次出現的位置,找不到返回 -1;
CString str(_T("abcabcd"));
int idx = str.FindOneOf(_T("cbd")); //idx 的值爲1;
④、字符串的替換與刪除:
Replace 替換 CString 對象中的指定的字符或者字符串,返回替換的個數,無匹配字符返回 0;
CString str(_T("abcdabc"));
int num = str.Replace('b', 'k'); //str == akcdakc, num == 2
CString str(_T("abcdabc"));
int num = str.Replace(_T("bc"), _T("kw")); //str == akwdakw, num == 2
Remove 刪除 CString 對象中的指定字符,返回刪除字符的個數,有多個時都會刪除;
CString str(_T("abcdabcb"));
int num = str.Remove('b'); //str == acdac, num == 3
Delete 刪除 CString 對象中的指定位置的字符,返回處理後的字符串長度;
CString str(_T("abcd"));
int num = str.Delete(1, 3); //str == a, num == 1
⑤、字符串的提取:
Left、Mid、Right 三個函數分別實現從 CString 對象的 左、中、右 進行字符串的提取操作;
CString str(_T("abcd"));
CString strResult = str.Left(2); //strResult == ab
strResult = str.Mid(1); //strResult == bcd
strResult = str.Mid(0, 2); //strResult == ab
strResult = str.Right(2); //strResult == cd
⑥、單個字符的修改:
GetAt、SetAt 可以獲取與修改 CString 對象中的單個 TCHAR 類型字符;
操作符也可以獲取 CString 對象中的單個字符,但爲只讀的,不能進行修改;
CString str(_T("abcd"));
str.SetAt(0, 'k'); //str == kbck
TCHAR ch = str.GetAt(2); //ch == c
⑦、其他類型與 CString 對象類型的轉換:
● 格式化字符串:Format 方法,實現從 int、long 等數值類型、TCHAR、TCHAR * 等類型向 CString 類型的轉換;
int num = 6;
CString str;
str.Format(_T("%d"), num);
● CString 類型向 int 等數值類型、TCHAR * 類型的轉換:
TCHAR *pszBuf = str.GetBuffer();
str.ReleaseBuffer();
TCHAR *p = (LPTSTR)(LPCTSTR)str;
CString str1(_T("123"));
int num = _ttoi(str1);
⑧、CString 對象的 Ansi 與 Unicode 轉換:
大家可以直接使用上節課給大家講解的方法,此外這裏給大家介紹一種從 Ansi 轉換到 Unicode 的隱含方法:
//當前工程環境爲Unicode
CString str;
str = "abc";
char *p = "defg";
str = p;
⑨、 CString 對象字符串所佔用的字節數:
CString str = _T("abc");
錯誤的求法:sizeof(CString)、sizeof(str)
正確的求法:str.GetLength()*sizeof(TCHAR)
⑩、當作爲 TCHAR * 類型傳參時,確保申請了足夠用的空間,比如使用 GetModuleFileName 函數;
放點測試代碼
#include <Windows.h>
#include <atlstr.h>
#include <iostream>
using namespace std;
int main(){
CString str1=_T("vaefgjoaefje");
CString str2(_T("adfghafec中文gget"));//含有中文
TCHAR szBuff[]=_T("sdfsfsd");
CString str3=szBuff;
CString str4(szBuff);
LPCTSTR P=_T("DSFSAG"); //
CString str5=P; //LPCTSTR(const wchar_t*) 直接轉換成CString
CString str6(P);
wcout <<"str1=" << str1.GetString() << endl;
wcout << "str1=" <<(LPCTSTR)str1 << endl;
wcout << "str1=" <<str1.GetBuffer() << endl;
wcout.imbue(std::locale("chs"));
wcout << "str2=" <<str2.GetString() << endl;
int len = str2.GetLength();
wcout << len << endl; //len=18
BOOL IsEmpty=str3.IsEmpty();
wcout << IsEmpty << endl; //0
str3.Empty();
IsEmpty=str3.IsEmpty();
wcout << IsEmpty << endl; //1
wcout << "str5=" <<str5.MakeLower().GetString() << endl;
wcout <<"str5=" <<str5.MakeUpper().GetString() << endl;
wcout << "str5=" <<str2.MakeReverse().GetString() << endl;
str1+=str2;
wcout << "str1=" << str1.GetString() << endl;
BOOL IsEqual = str1==str2;
wcout << IsEqual << endl; //0
IsEqual=str1!=str2;
wcout << IsEqual << endl; //1
int n = str1.Find(_T("ef"),0);
wcout << n << endl; //2
n = str1.Find(_T("ef"),2);
wcout << n << endl; //2
n=str1.Find(_T("ef"),3);
wcout << n << endl;
n=str1.ReverseFind('e'); //ReverseFind()只能查找字符,字符串不行
wcout << n << endl;
n=str1.FindOneOf(_T("*tg"));
wcout << n << endl;
wcout << str1.GetString() << endl;
n=str1.Replace(_T("ef"),_T("fe"));
wcout << str1.GetString() << endl;
wcout << n << endl;
n=str1.Replace('e','f');
wcout << "n=" << n << ' ' << "str1=" << str1.GetString() << endl;
n=str1.Remove('f'); //只能刪除字符
wcout << "n=" << n << ' ' << "str1=" << str1.GetString() << endl;
n=str1.Delete(5,3); //只能刪除一截
/*wcout << "n=" << n << ' ' << "str1=" << str1.GetString() << endl;
n=str1.Delete(0,str1.GetLength());*/
wcout << "n=" << n << ' ' << "str1=" << str1.GetString() << endl;
n=str1.Replace(_T("gg"),_T("")); //可用來刪除字符串中所有指定的子字符串
wcout << "n=" << n << ' ' << "str1=" << str1.GetString() << endl;
str1=str1.Left(str1.Find(_T("中"),0)); //從第一個字符開始數指定數字個字符,結果就是這個位置往左所有的字符串,不包含指定位置的
wcout << "str1=" << str1.GetString() << endl;
str1=str1.Right(str1.Find(_T("文"),0)); //從右往左數指定數字個字符,
wcout << "str1=" << str1.GetString() << endl;
str1=str1.Mid(str1.Find(_T("g"),0),3); //從指定位置(第一個參數)開始數指定個數(第二個參數)字符,包含指定位置
wcout << "str1=" << str1.GetString() << endl;
str1.SetAt(1,str1.GetAt(2));
wcout << "str1=" << str1.GetString() << endl;
TCHAR *char1 = (LPTSTR)str1.GetString(); //GetString結果是LPCTSTR
wcout << char1 << endl;
char1 = str1.GetBuffer(); //GetBuffer結果是LPTSTR
wcout << char1 << endl;
CString str7(_T("134"));
n=_ttoi(str7);
wcout << n << endl;
LPCTSTR p1 = str1;
LPTSTR p2=(LPTSTR)(LPCTSTR)str1; //CString可以先強轉到LPCTSTR,再次強轉成LPTSTR
CString str8(_T("important"));
CString str10(_T("attention"));
CString str9;
float f1=1.06;
CString time;
str9.Format(_T("this function is %s, %s please,version:%.2f \n"),str8,str10,f1);
wcout << str9.GetBuffer() << endl;
getchar();
return 0;
}