gpt4 book ai didi

c++ - 如何在 C++ 中获取 a 字符串的一部分?

转载 作者:可可西里 更新时间:2023-11-01 17:11:04 31 4
gpt4 key购买 nike

如何在 C++ 中获取字符串的一部分?我想知道从 0 到 i 的元素是什么。

最佳答案

您想使用 std::string::substr。这是一个例子,无耻地复制自 http://www.cplusplus.com/reference/string/string/substr/

// string::substr
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str="We think in generalities, but we live in details.";
// quoting Alfred N. Whitehead
string str2, str3;
size_t pos;

str2 = str.substr (12,12); // "generalities"

pos = str.find("live"); // position of "live" in str
str3 = str.substr (pos); // get from "live" to the end

cout << str2 << ' ' << str3 << endl;

return 0;
}

关于c++ - 如何在 C++ 中获取 a 字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2498119/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com