[关闭]
@RabbitHu 2017-09-19T07:33:43.000000Z 字数 2006 阅读 6666

十个你一定会用到的 C++ string 函数!

笔记


目录

  1. 赋值         6. 交换
  2. 长度         7. 子串
  3. 比较         8. 替换
  4. 查找         9. 插入
  5. 连接         10. 删除

赋值

将字符串2赋值给字符串1:
1. 字符串1 = 字符串2;
2. 字符串1.assign(字符串2);

  1. string s1 = "I'm ";
  2. string s2;
  3. s2 = "1234Juruo1234";
  4. s1.assign(s2);
  5. cout << s1;
  6. //输出结果:1234Juruo1234

将字符串2从第m个字符开始的n个字符赋值给字符串1:
字符串1.assign(字符串2, m, n);

  1. string s1 = "I'm ";
  2. string s2 = "1234Juruo1234";
  3. s1.assign(s2, 4, 5);
  4. cout << s1;
  5. //输出结果:Juruo

长度

返回字符串长度:
1. 字符串1.length();
2. 字符串1.size();

  1. string s1 = "1234Juruo1234";
  2. cout << s1.length() << endl;
  3. cout << s1.size() <<endl;
  4. /*
  5. 输出结果:
  6. 13
  7. 13
  8. */

比较

">", "<", "==", ">=", "<="均可以用于字符串比较。

  1. string PPAP[] = {"I", "have", "a", "pen", "an", "apple", "um", "apple-pen"};
  2. sort(PPAP, PPAP + 8);
  3. for(int i = 0; i < 8; i++){
  4. cout << PPAP[i] << endl;
  5. }
  6. /*
  7. 输出结果:
  8. I
  9. a
  10. an
  11. apple
  12. apple-pen
  13. have
  14. pen
  15. um
  16. */

查找

在字符串1中从第m个字符开始查找字符串2,返回第一次出现的首字母位置,失败时返回-1:
字符串1.find(字符串2, m);

  1. string s1 = "ggabcdabcgggabcdefg";
  2. string s2 = "gg";
  3. int pos = -1;
  4. while(1){
  5. pos = s1.find(s2, pos+1);
  6. if(pos == -1) break;
  7. cout << pos << ' ';
  8. }
  9. //输出结果:0 9 10

在字符串1中从第m个字符开始从后向前查找字符串2,返回第一次出现的首字母位置,失败时返回-1:
字符串1.rfind(字符串2, m);

  1. string s1 = "ggabcdabcgggabcdefg";
  2. string s2 = "gg";
  3. int pos = s1.length();
  4. while(pos > 0){
  5. pos = s1.rfind(s2, pos-1);
  6. if(pos < 0) break;
  7. cout << pos << ' ';
  8. }
  9. //输出结果:10 9 0;

连接

将字符串2接到字符串1尾部:
1. 字符串1.append(字符串2); //字符不可
2. 字符串1 += 字符串2; //字符亦可

  1. string s1 = "I'm ";
  2. string s2 = "Juruo";
  3. s1.append(s2);
  4. // 或 s1 += s2;
  5. cout << s1;
  6. //输出结果:I'm Juruo

将字符串2从第m个字符开始的n个字符接到字符串1尾部:
字符串1.append(字符串2, m, n);

  1. string s1 = "I'm ";
  2. string s2 = "1234Juruo1234";
  3. s1.append(s2, 4, 5);
  4. cout << s1;
  5. //输出结果:I'm Juruo

交换

字符串1.swap(字符串2);

  1. string s1 = "I'm ";
  2. string s2 = "Juruo";
  3. s1.swap(s2);
  4. cout << s1 << endl;
  5. //输出结果:Juruo

子串

返回字符串1从第m个字符开始的n个字符所组成的子串:
字符串1.substr(m, n);

  1. string s1 = "I'm ";
  2. string s2 = "1234Juruo1234";
  3. s1 = s2.substr(4, 5);
  4. cout << s1 << endl;
  5. //输出结果:Juruo

替换

在字符串1中删除从m开始的n个字符,然后在m处插入串s2
字符串1.replace(m, n, s2);

  1. string s1 = "I'm Juruo";
  2. string s2 = "Juruo";
  3. string s3 = "Dalao";
  4. int pos = s1.find(s2);
  5. s1.replace(pos, s2.length(), s3);
  6. cout << s1;
  7. //输出结果:I'm Dalao

插入

在字符串1的第m个字符处插入字符串2:
字符串1.insert(m, 字符串2);

  1. string s1 = "I'm Juruo";
  2. string s2 = "not ";
  3. s1.insert(s1.find("Juruo", 0), s2 );
  4. cout << s1 << endl;
  5. //输出结果:I'm not Juruo

删除

从字符串1的第m个字符开始,删除n个字符:
字符串1.erase(m, n);

  1. string s1 = "I'm not Dalao";
  2. s1.erase(s1.find("not"), 4);
  3. cout << s1 << endl;

作者:胡小兔 //←这个网站经常崩溃
批评指正敬请联系:yuanxiaodidl@163.com
打赏一根胡萝卜:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注