string容器有没有可以截取指定位置到指定位置的子字符串函数
是的,C++的string容器有提供截取指定位置到指定位置的子字符串的函数。可以使用substr()函数来实现。
substr()函数的原型如下:
string substr (size_t pos, size_t len) const;
其中,pos表示截取子字符串的起始位置,len表示截取的子字符串的长度。
示例代码如下:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::string subStr = str.substr(7, 5); // 从位置7开始截取长度为5的子字符串
std::cout << subStr << std::endl; // 输出 "World"
return 0;
}
这样就可以截取出指定位置到指定位置的子字符串了。
原文地址: https://www.cveoy.top/t/topic/hUT0 著作权归作者所有。请勿转载和采集!