①、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;
}