内容:从string字符串中获得想要的子串。
(ps:明天白天要上课玩手机,白天的空闲时间要收行李,晚上又要去赶高铁,回到家应该会很晚了,有可能要被迫咕咕了啊)
#include <iostream>
#include <string>
using namespace std;
/*
1. string substr(int pos = 0, int n = npos) const;
(返回从pos位置开始的由n个字符组成的字符串,默认返回全部字符串)
2. 字符串首位置为0
*/
void test01()
{
string str = "abcdefg";
string str1 = str.substr(); // here
cout << str1 << endl;
string str2 = str.substr(1, 5); // here
cout << str2 << endl;
}
// 操作案例:通过邮箱来获得用户名
void test02()
{
string email = "xiaoming@qq.com";
int pos = email.find('@');
string name = email.substr(0, pos); // here
cout << name << endl;
}
int main()
{
test01();
test02();
return 0;
}