c++ substr函数的有哪些作用
C++中的substr函数用于从一个字符串中获得子字符串。它的作用是从指定位置开始,截取字符串的一部份,并返回该子字符串。
substr函数的语法为: string substr (size_t pos, size_t len) const;
其中,pos是要开始截取的位置,len是要截取的子字符串的长度。如果省略len参数,则默许截取从pos位置到字符串末尾的所有字符。
例如,对字符串"Hello, World!",我们可使用substr函数来获得其中的子字符串: string str = “Hello, World!”; string sub = str.substr(7, 5);
上述代码将截取字符串str中从位置7开始长度为5的子字符串,即"World"。终究,子字符串将存储在变量sub中。
TOP