1、对象连接
CString s1, s2, s3;
s1 = _T("Hello");
s2 = _T("World");
s3 = s1 + _T(",") + s2 +_T("!");
MessageBox(s3);
2、格式化
CString str;
int dollar = 100;
str.Format(_T("I have %d dollars\n"), dollar);
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!
3、构造函数
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个给定的字符初始化类对象
4、大小写转换及顺序转换函数
CString s1 = _T("aaBBCCddEe");
s1.MakeLower();
MessageBox(s1); //输出 aabbccddee s1.MakeUpper();
MessageBox(s1); //输出 AABBCCDDEE
MessageBox(s1.MakeLower().MakeReverse()); //输出eeddccbbaa
MessageBox(s1); //输出eeddccbbaa
5、对象的比较
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);
6、对象字符串的提取
CString s1 = _T("aabbccddee"),s2;
S2=s1.Left(4);
MessageBox(s1.Left(4)); //左边四个字符,输出aabb
MessageBox(s1.Right(4)); //右边4个字符,输出ddee
MessageBox(s1.Mid(4, 2)); //输出从第4个位置开始的两个字符,即输出cc
MessageBox(s1.Mid(4)); //输出从第4个位置开始到结尾的子串,即输出ccddee
7、查找
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);
8、替换与删除
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
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
9、插入字符
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);
10、CString 、int互转
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);
11、CString、char*互转
CString s1 = _T("Hello,World!");
CString s2;
s2.Format(_T("Hello,World!"));
CString str1 ="123";
char *p =(LPSTR)(LPCSTR)str1;
char *t1 =str1.GetBuffer(str1.GetLength());
str1.ReleaseBuffer();
char *str ="aaaa"
CString str1(str);