【模板】字符串小结
关于字符串的一些用法:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main(){ 5 /* 6 char转string 7 char s1[3]; 8 cin>>s1; 9 cout<<s1[2]<<endl; 10 string s2 = s1; 11 cout<<s2; 12 */ 13 string ss = "ABC"; 14 char sa[100]; 15 strcpy(sa, ss.c_str()); 16 cout<<sa[2]<<endl;//string转char 17 18 string s1, s2, s3, s4; 19 s1 = ss.substr(1, 1); 20 s2 = ss.insert(1, "EF"); 21 s3 = ss.erase(2, 1); 22 s4 = ss.replace(1, 1, "MN"); 23 cout<<s1<<" "<<s2<<" "<<s3<<" "<<s4<<endl; 24 //ss = s3+s4; 25 cout<<ss<<endl; 26 if(ss==s4)cout<<"same"<<endl; 27 int p1, p2, p3; 28 p1 = ss.find("aNqE"); //返回-1就是没找到 29 p2 = ss.rfind('C'); //最后一个关键字出现位置 30 p3 = ss.find_first_of("AC");//任何一个首次在str1中出现的位置 31 cout<<p1<<" "<<p2<<" "<<p3<<endl; 32 33 return 0; 34 }