//从字符串中获取想要的子串
//函数原型:
//string substr(int pos = 0,int n = npos)const;
//返回由pos开始的n个字符组成的字符串
#include<iostream>
using namespace std;
void test01() {
string str = "abcdefg";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
}
void test02() {
//实用操作
string email = "hello@sina.com";
//从邮件地址中获取用户名信息
//首先,明确邮箱共性,标志符:@
//这里括号内的@,使用'@'或者"@"都可以
int pos = email .find("@");
//pos位置的值不计入substr()
string subStr = email.substr(0, pos);
cout << "subStr = " << subStr << endl;
}
int main() {
test01();
test02();
}
|