功能描述:

    • 从字符串中获取想要的子串

    函数原型:

    1. string substr(int pos = 0,int n = npos) const; //返回由pos开始的n个字符组成的字符串
    1. // string求子串
    2. void test01()
    3. {
    4. string str = "abcdef";
    5. string subStr = str.substr(1, 3);
    6. cout << "sbuStr = " << subStr << endl;
    7. }
    8. // 实用操作
    9. void test02()
    10. {
    11. string email = "zhangsan@sina.com";
    12. //从邮件地址中 获取 用户名信息
    13. int pos = email.find("@");
    14. string username = email.substr(0, pos);
    15. cout << "username = " << username << endl;
    16. }
    17. int main(void)
    18. {
    19. test01();
    20. test02();
    21. system("pause");
    22. return 0;
    23. }