代码
#include<iostream>#include<string>using namespace std;//从字符串中获取想要的字串//函数原型/*① string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串*/void test01(){string s1 = "abcdef";string s2 = s1.substr(1, 3);cout << "s2 = " << s2 << endl;}//实操void test02() {string emal = "zhangsan@qq.com";//从邮箱中获取用户名信息int pos = emal.find("@");string userName = emal.substr(0, pos);cout << "userName = " << userName << endl;}int main() {test01();test02();return 0;}
运行结果

