如何在 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/