C++ 字符串截取: 如何提取 'FF' 从 'FCFC'
C++ 字符串截取: 如何提取 'FF' 从 'FCFC'
在 C++ 中,可以使用 substr() 函数来截取字符串。要截取字符串中的 'FF',可以使用以下代码:
#include <iostream>
#include <string>
int main() {
std::string str = "FCFC";
std::string subStr = str.substr(0, 2);
std::cout << subStr << std::endl;
return 0;
}
这段代码中,我们使用 substr() 函数来截取字符串的前两个字符,从索引 0 开始,长度为 2。然后将截取得到的子字符串输出到控制台。
输出结果为:
FF
这样就成功截取并获得了字符串中的 'FF'。
原文地址: https://www.cveoy.top/t/topic/fvZ0 著作权归作者所有。请勿转载和采集!