1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <iostream> #include <string> using namespace std;
int main() { string str1="foo"; string str2="bar"; string str3=str1+str2; cout <<"str3 = "<<str3 <<endl; str2+=str1; cout <<"str2 = "<<str2 << endl;
str3 = "hello, china!"; string str4("hello, zju!"); string str5(str3); string str6(str3, 7, 5); cout <<"str4 = "<<str4 <<endl; cout <<"str5 = "<<str5 <<endl; cout <<"str6 = "<<str6 <<endl;
string str7 =str3.substr(7,5); cout <<"str7 = " << str7 << endl; string str8 = str3; str8.replace(7,5,"hangzhou"); cout <<"str8 = "<< str8 <<endl;
str8.assign(10, 'A'); cout <<"str8 = "<< str8 <<endl;
string str9 = "hello, hangzhou city"; cout << "str9 = " << str9 << endl; string str_to_find = "hangzhou"; cout << str9.find(str_to_find) << endl; str9.replace(str9.find(str_to_find),str_to_find.length(),"beijing"); cout <<"str9 = "<< str9 <<endl; }
|