我怎样才能从字符串中得到一个单词

How can I get a word from string

本文关键字:单词 一个 字符串      更新时间:2023-10-16

我有f.e."我是一名护士。我或哪个函数如何使用将单词从字母 1 获取到空格或字母 11?

所以我应该得到"正在工作"

要从流中使用运算符>>在字符串上读取单词

std::stringstream  stream("I am working as a nurse.");
std::string  word;
stream >> word;  // word now holds 'I'
stream >> word;  // word now holds 'am'
stream >> word;  // word now holds 'working'
// .. etc

目前还不完全清楚您想要什么,但从您的示例中,您似乎想要从字符 1 开始并在字符 11 位之后结束的子字符串(总共 12 个字符)。这意味着您需要string::substr

std::string str("I am working as a nurse");
std::string sub = str.substr(1,12);
char[] source = "I am working as a nurse."
char[11] dest;
strncpy(dest, &source[1], 10)
相关文章: