# include<cstdio> # include<iostream> # include<string> using namespace std; int main(void) { string s1("Hello"); string s2(8,'x'); string month = "March"; string ob; cin>>ob;//string 支持流读取运算符 string s;//string 支持getline函数 getline(cin,s); string s1("cat"),s2; s2 = s1;//用等号赋值 string s1("cat"),s3; s3.assign(s1);//用assign()成员函数进行赋值 string s1("cartsfasf"),s3; s3.assign(s1,1,3);//把s1中下标为1开始的3个字符赋值给s3 s2[5] = s1[3] = 'k';//单个字符的赋值 string s1("good"),s2("morning"); s1+=s2; cout<<s1; string s1("hello world"),s2; s2 = s1.substr(4,5); cout<<s2<<endl; string s1("hello world"); cout<<s1.find("ll",1)<<endl; cout<<s1.find("ll",2)<<endl; string s1("hello world"); s1.replace(2,3,"haha"); cout<<s1;// hehaha world string s1("hellow world"); string s2("show insert"); s1.insert(5,s2); cout<<s1<<endl; s1.insert(2,s2,5,3); cout<<s1<<endl; string s1("hellow"),s2("hellow"),s3("hell"); bool b = ( s1==s2 ); cout<<b<<endl; bool b = ( s1==s3 ); cout<<b<<endl; bool b = ( s1>s3 ); cout<<b<<endl; //逐个访问string对象中的字符 string s1("hellow"); for ( int i = 0;i < s1.length();i++ ) { cout<<s1.at(i)<<endl; } cout<<s1<<endl; cout<<s1.length()<<endl;//string 对象的成员函数用length()读取 cout<<s2<<endl; cout<<month<<endl; return 0; }