#include<iostream> using namespace std; #include"string" #include"algorithm" int main() { string s1 = "who are you ?"; string s2(" i am your father."); s1 += s2; cout <<"s1 is : " << s1 << endl; cout <<"s1[2] is : "<< s1[2]<< endl; //出现异常不会抛出异常 cout << "s2.at(2) is : " << s1.at(2) << endl; //出现异常会抛出异常 cout << " the size of s2 is :" << s2.size() << endl; cout << " the length of s2 is :" << s2.length() << endl;//两者没区别,且长度中都没有算‘\0‘ //查找和替换 int iFind = s1.find( "o",0); while (iFind != string::npos) { cout << "o 的位置在 : " << iFind << endl; s1.replace(iFind, 1, "O"); iFind += 1; iFind = s1.find("o", iFind); } cout << " 替换后的s1 :"<<s1<<endl; //删除 string::iterator it = find(s1.begin(), s1.end(), ‘i‘); if (it != s1.end()) { s1.erase(it); } cout << "删除后的s1 是: " << s1 << endl; //插入 s1.insert(s1.begin(),‘A‘); cout << "插入A 后的s1 :" << s1 << endl; transform(s1.begin(), s1.end(), s1.begin(), toupper); cout << " s1 大写之后 : " << s1 << endl; transform(s1.begin(), s1.end(), s1.begin(), tolower); cout << " s1 小写之后 : " << s1 << endl; //拷贝 char buf1[11] = { 0 }; s1.copy(buf1, 3, 0); cout << " buf1 is :" << buf1 << endl; system("pause"); }
时间: 2024-11-05 12:25:19