C++ Cstring类型使用

#include 
#include 

using namespace std;

int main(void)
{
	CString str("hello");
	
	cout << str.GetLength() << endl;//得到字符串长度,不包括'\0'

	cout << str.GetAt(4) << endl;//得到单个字符下标

	cout << str.IsEmpty() << endl;//判断字符串是否为空,1为空,0不为空

	str.SetAt(4,'w');//设置下标单个字符
	cout << str << endl;

	cout << str.Mid(2,2) << endl;//从第二个位置获取后两个字母

	cout << str.Left(3) <<	endl;//从左边第三个往左边获取三个字母

	cout << str.Right(3) << endl;//从右边第三个往右边获取三个字母

	cout << str.MakeUpper() << endl;//将字符串全部转换成大写

	cout << str.MakeLower() << endl;//将字符串全部转换成小写

	cout << str.MakeReverse() << endl;//将字符串倒序

	CString str2;
	str2.Format("string:%s, float:%f, int:%d, hex:%x, char:%c","hello",3.14,11,0xFF,65);//格式化输出字符串
	cout << str2 << endl;

	str.Empty();//清空所有字符
	cout << str.GetLength() << endl;



	system("pause");
	return 0;
}

运行结果:
5
o
0
hellw
ll
hel
llw
HELLW
hellw
wlleh
string:hello, float:3.140000, int:11, hex:ff, char:A
0
请按任意键继续. . .

你可能感兴趣的:(学习笔记)