string定义在命名控件std中,所以需要在文件中引入命名空间
using namespace std;
定义和初始化string
string s1 = "";
string s2 = s1;
string s3 = "Hello World";
string s4(s3);
string s5("world");
string s6(4, 'A');
判断是否为空
string str = "";
if(str.empty())
{
cout<<"str is empty"<<endl;
}else{
cout<<"str is not empty"<<endl;
}
字符串长度
string str = "abcd";
int len = str.size();
遍历字符串
string str = "Hello World";
for(auto s : str)
{
cout<<s<<endl;
}
字符串拼接
string str1 = "hello";
string str2 = "world";
string str3 = str1 + " "+ str2;
cout<<str3<<endl

1万+

被折叠的 条评论
为什么被折叠?



