代码

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. //从字符串中获取想要的字串
  5. //函数原型
  6. /*
  7. ① string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
  8. */
  9. void test01(){
  10. string s1 = "abcdef";
  11. string s2 = s1.substr(1, 3);
  12. cout << "s2 = " << s2 << endl;
  13. }
  14. //实操
  15. void test02() {
  16. string emal = "zhangsan@qq.com";
  17. //从邮箱中获取用户名信息
  18. int pos = emal.find("@");
  19. string userName = emal.substr(0, pos);
  20. cout << "userName = " << userName << endl;
  21. }
  22. int main() {
  23. test01();
  24. test02();
  25. return 0;
  26. }

运行结果

image.png