整理了一些C++中string类的相关操作函数及运算符。
注意其中用到了c11的列表初始化,如果编译不通过则把初始化时的花括号替换为小括号即可。
-
初始化
void init(){ //创建string对象 string str1{"HelloWorld!"}; char ch[] = "helloworld!"; string str2{ch}; }
-
追加字符串
void test1(){ //在字符串末尾追加 string s1{"Hello"}; s1.append("World!"); //末尾追加字符串 cout << s1 << endl; //HelloWorld! string s2{"Welcome"}; s2.append(" to C and C++",3,2); //从下标3的字符开始拷贝2个字符,即" C" cout << s2 << endl; //welcome C string s3{"Welcome"}; s3.append(" to C and C++",5); //从头开始拷5个字符,即" to C" cout << s3 << endl; //Welcome to C string s4{"Welcome"}; s4.append(3,'6'); //把3个6追加到s4的末尾 cout << s4 << endl; //Welcome666 }
-
为字符串赋值
void test2(){ //给字符串赋值 string s1{"Welcome"}; s1.assign("HelloWorld!"); //把"HelloWorld!"赋值给s1 cout << s1 << endl; //HelloWorld! string s2{"Welcome"}; s2.assign("HelloWorld!",5,3); //把从下标5的字符开始往后3个字符赋值给s2 cout << s2 << endl; //Wor string s3{"Welcome"}; s3.assign("HelloWorld!",5); //从头开始拷5个字符赋值给s3 cout << s3 << endl; //Hello string s4{"Welcome"}; s4.assign(3,'6'); //把3个'6'赋值给s4 cout << s4 << endl; }
-
比较字符串
void test3(){ //比较字符串 string s1{"Welcome"}; string s2{"HelloWorld"}; cout << s1.compare(s2) << endl; //1 cout << s2.compare(s1) << endl; //-1 cout << s1.compare("Welcome") << endl; //0 }
-
获取子串
void test4(){ //获取子串 string s1{"HelloWorld!"}; cout << s1.substr(0,1) << endl; //从下标0的字符开始往后一个字符,即"H" cout << s1.substr(3) << endl; //从下标5的字符开始,直到末尾,即"World!" }
-
搜索字符串
void test5(){ //搜索字符串 string s1{"Welcome to C and C++"}; cout << s1.find("co") << endl; //返回子串出现的第一个位置下标,即3 cout << s1.find(' ') << endl; //返回字符' '第一次出现的位置,即7 cout << s1.find(' ',8) << endl; //返回字符' '从下标8开始第一次出现的位置,即10 //返回子串从下标7开始第一次出现的位置,没找到就返回string::npos,也可以说是0xffffffff,所以输出"没找到" if(s1.find("co",7)==string::npos)cout << "没找到" << endl; //也可以和0xffffffff比较,等效 else cout << s1.find("co",7) << endl; }
-
插入和替换字符串
void test6(){ //插入和替换字符串 string s1{"Welcome to C++"}; s1.insert(11,"Java and "); //在下标11的字符前面插入字符串"Java and " cout << s1 << endl; //Welcom to Java and C++ string s2{"AA"}; s2.insert(1,4,'C'); //在下标1的字符前面插入4个字符'C' cout << s2 << endl; //ACCCCA string s3{"Welcome to Java"}; s3.replace(11,4,"C++"); //从下标11的字符开始往后四个字符替换为"C++" cout << s3 << endl; //Welcome to Java }
-
函数at erase clear empty
void test7(){ //函数at erase clear empty string s1{"Welcome"}; cout << s1.at(3) << endl; //返回下标为3的字符,如果越界则输出相关异常提示,当前输出c s1.erase(2,3); //删除从下标2开始往后3个字符,即lco cout << s1 << endl; //Weme s1.clear(); //清空字符串,清空后为一个空串 cout << s1 << "#" << endl; //# if(s1.empty())cout << "空串" << endl; //空串 else cout << "没空" << endl; }
-
字符串运算符
void test8(){ //字符串运算符 /* [ ] 用数组下标运算符访问字符串中的字符 = 将一个字符串的内容复制到另一个字符串 + 连接两个字符串得到一个新串 += 将一个字符串追加到另一个字符串末尾 << 将一个字符串插入一个流 >> 从一个流提取一个字符串,分界符为空格或者空结束符 ==, !=, <, <=, >, >= 用于字符串比较 */ string s1 = "ABC"; string s2 = s1; cout << s2 << endl; //ABC string s3 = s2+"DEFG"; cout << s3 << endl; //ABCDEFG s1+="DE"; //ABC+DE cout << s1 << endl; //ABCDE s1 = "ABC"; s2 = "ABE"; cout << (s1 == s2) << endl; //0 cout << (s1 != s2) << endl; //1 cout << (s1 > s2) << endl; //0 cout << (s1 >= s2) << endl; //0 cout << (s1 < s2) << endl; //1 cout << (s1 <= s2) << endl; //1 }
参考文献
中国大学mooc中c++程序设计(面向对象进阶)第1.2 课件讲解 -string类