- Cstring类
- CstringArray类
- CstringList类
- Ctiem类
- Cstring类
Cstring提供了多种定义和赋值形式,下面通过范例来说明:
<1>一般形式
CString str1;//定义一个CString对象str1 CString str2("a string");//定义一个CString对象,在紧跟其后的括号内用字符串给其赋值 str1 = str2;//用运算符"="给对象赋值 CString str3 ="this is the third string"; CString str4( str3);//定义一个CString对象,在紧跟其后的括号内用字符串对象给其赋值 cout << "str1="<<(LPCTSTR)str1<<endl;//输出str1 cout << "str2="<<(LPCTSTR)str2<<endl;//输出str2 cout << "str3="<<(LPCTSTR)str3<<endl;//输出str3 cout << "str4="<<(LPCTSTR)str4<<endl<<endl;//输出str4<2>访问Cstring类的单个字符
可使用getat和setat成员函数访问Cstring对象中的单个字符,还可以采用访问数组元素相似的方法,利用运算符 [] 访问当个字符值。
str2.SetAt(0,'c'); //str2变成“c string” char c = str2.GetAt(0); //c的值为‘c’; cout<<"str2="<<(LPCTSTR)str2<<endl; cout<<"C="<<c<<endl;<3>字符串的串联
CString str1="this"; //str1="this" CString str2=str1+""; //str2="this " str2+="is"; //str2="this is" CString str3("a string"); //str3="a string" str2+=""; //str2="this is " str2+=str3; //str2="this is a string" cout<<"str2="<<(LPCTSTR)str2<<endl; //输出“this is a string”