int main()
{
//使用字符数组
char str[]="123";
cout << str << endl;
//使用字符指针
const char* s = "joker";
cout << s << endl;
//直接使用字面值
cout<<"joker"<<endl;
//使用字符串对象
string s1("12z");
string s2 = "123";
string* s3 = new string("abcde");
string s4 = s2;
cout << s1 << "\t" << s2 << "\t" << *s3 << "\t" << endl;
//复制
s1 = s2;
cout << s1 << endl;
s4 += s2;
cout << s4 << endl;
//比较
if (s1 == s4)
cout << "yes"<< endl;
s4.append("200");
s4.push_back('!');
cout << s4 << endl;
//交换
s1.swap(s4);
cout << s1 << endl;
cout << s4 << endl;
//长度
cout << s1.size() << endl;
cout << s1.length()<< endl;
//替换
s1.replace(s1.find("32", 0), 6, "abcde");
cout << s1 << endl;
delete s3;
return 0;
}
string类
最新推荐文章于 2024-09-16 17:46:36 发布
本文介绍了C++中字符数组、字符指针、字符串对象以及基本操作,如输出、复制、比较、交换、长度获取和字符串替换,并展示了如何使用`find`和`replace`方法进行操作。
摘要由CSDN通过智能技术生成