c++的substring函数怎么用
在C++中,可以使用substr()函数来获取一个字符串的子字符串。该函数接受两个参数,第一个参数是子字符串的起始位置(以0为索引),第二个参数是子字符串的长度。
下面是一个使用substr()函数的示例:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 获取从索引6开始的子字符串
std::string sub1 = str.substr(6);
std::cout << sub1 << std::endl; // 输出 " World!"
// 获取从索引0开始长度为5的子字符串
std::string sub2 = str.substr(0, 5);
std::cout << sub2 << std::endl; // 输出 "Hello"
return 0;
}
在上述示例中,我们使用substr()函数获取了两个子字符串:从索引6开始的子字符串" World!"和从索引0开始长度为5的子字符串"Hello"
原文地址: https://www.cveoy.top/t/topic/inmm 著作权归作者所有。请勿转载和采集!