关注小韩 轻松编程 一起由浅入深
#include<iostream>
using namespace std;
#include<string>
// string求子字符串
void test() //获取字串案例
{
string str = "abcdef";
string newStr=str.substr(1, 3); //要创建新的变量接收获取的子符串 两个参数一个起始位置 一个截取长度
cout << newStr << endl; //注意输出的是获取后的子字符串subStr
cout << str << endl; //由此可知 substr() 函数是获取子字符串 不是截取
}
//实用操作
void test1()
{
string str1 = "981924395@qq.com"; //作者QQ邮箱哦
//从邮箱中获取相关信息
int pos = str1.find("@"); //获取作者QQ号(号码在@前面)
string newStr1=str1.substr(0, pos);
cout << newStr1 << endl;
}
int main()
{
test();
test1();
return 0;
}
如有疑问欢迎评论区留言哦