c++中的substr函数

substr函数属于string类的一个函数

语法:

#include <string>
basic_string substr( size_type index, size_type num = npos );

用法:
substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。

举例:

    string s("What we have here is a failure to communicate");

    string sub = s.substr(21);

    cout << "The original string is " << s << endl;
    cout << "The substring is " << sub << endl;

输出:

    The original string is What we have here is a failure to communicate
    The substring is a failure to communicate

猜你喜欢

转载自blog.csdn.net/rookiemonkey/article/details/79710607