如何从字符串中删除第一个单词?

4

假设我有

string sentence{"Hello how are you."}

我希望将字符串中的“Hello”替换为“你好”,得到的字符串仍然包含“how are you”。应该如何实现?

我尝试了以下代码:

stringstream ss(sentence);
ss>> string junkWord;//to get rid of first word

但是当我这样做时:

cout<<sentence;//still prints out "Hello how are you"

很明显,stringstream 不会改变实际的字符串。我也尝试使用 strtok 但它与 string 不兼容。

将字符串分割成单词(通过stringstream),然后重新添加除第一个单词以外的所有单词,这个怎么样? - arc_lupus
1
你只需要使用一个向量,不需要循环。看这里:https://dev59.com/k3VC5IYBdhLWcg3wnCj6 - arc_lupus
@MatthiasB 试一下 - Dmitry Ledentsov
1
@DmitryLedentsov 是的,我注意到了。我想错方向了。当然需要的是 std::getline:http://ideone.com/IxZRAt 。不过在任何情况下我都不会使用这种方法... - MatthiasB
谢谢大家的帮助,我弄明白了! - user3247278
显示剩余2条评论
8个回答

7
请尝试以下操作:
#include <iostream>
#include <string>

int main() 
{
    std::string sentence{"Hello how are you."};

    std::string::size_type n = 0;
    n = sentence.find_first_not_of( " \t", n );
    n = sentence.find_first_of( " \t", n );
    sentence.erase( 0,  sentence.find_first_not_of( " \t", n ) );

    std::cout << '\"' << sentence << "\"\n";

    return 0;
}

输出结果为:
"how are you."

4
str=str.substr(str.find_first_of(" \t")+1);

测试完成:

string sentence="Hello how are you.";
cout<<"Before:"<<sentence<<endl;
sentence=sentence.substr(sentence.find_first_of(" \t")+1);
cout<<"After:"<<sentence<<endl;

执行:

> ./a.out
Before:Hello how are you.
After:how are you.

假设该行不以空格开头。在这种情况下,这种方法将无效。
find_first_of("<list of characters>").

在我们的情况下,字符列表包括空格和制表符。这将搜索任何列表中的第一个出现并返回一个迭代器。之后,添加 +1 会将位置移动一个字符。然后,位置指向行的第二个单词。Substr(pos) 将提取从位置开始直到字符串的最后一个字符的子字符串。


看看 Vlad 的回答,了解如何处理前导空格。 - pmr
谢谢Vijay!最终每个人的答案都很到位,我真的很感激大家的帮助。Vijay,你的解决方案最简单、最高效,我非常感谢你的帮助。你能快速解释一下:find_first_of(" ")+1吗?再次感谢! - user3247278

1
你可以例如取剩余的子字符串。
string sentence{"Hello how are you."};
stringstream ss{sentence};
string junkWord;
ss >> junkWord;
cout<<sentence.substr(junkWord.length()+1); //string::substr

然而,这也取决于您想进一步做什么。


我更喜欢再次使用字符串句子变量,而不仅仅是打印剩余的字符串。我对substr不太熟悉,也不太清楚你在做什么junkWord.length()+1。 - user3247278

1

有无数种方法可以做到这一点。我认为我会选择这个:

#include <iostream>
#include <string>

int main() {
    std::string sentence{"Hello how are you."};

    // First, find the index for the first space:
    auto first_space = sentence.find(' ');

    // The part of the string we want to keep
    // starts at the index after the space:
    auto second_word = first_space + 1;

    // If you want to write it out directly, write the part of the string
    // that starts at the second word and lasts until the end of the string:
    std::cout.write(
        sentence.data() + second_word, sentence.length() - second_word);
    std::cout << std::endl;

    // Or, if you want a string object, make a copy from the start of the
    // second word. substr copies until the end of the string when you give
    // it only one argument, like here:
    std::string rest{sentence.substr(second_word)};
    std::cout << rest << std::endl;
}

当然,除非你有非常好的理由不这样做,否则你应该检查first_space != std::string::npos,这意味着空格未被找到。为了清晰起见,我的示例代码中省略了这个检查 :)

0
你可以使用 string::find() 来查找第一个空格。一旦你得到了它的索引,就可以使用 string::substr() 从空格之后的索引开始获取子字符串,一直到字符串的末尾。

0
一句话:
std::string subStr = sentence.substr(sentence.find_first_not_of(" \t\r\n", sentence.find_first_of(" \t\r\n", sentence.find_first_not_of(" \t\r\n"))));

工作示例:

#include <iostream>
#include <string>

void main()
{
    std::string sentence{ "Hello how are you." };

    char whiteSpaces[] = " \t\r\n";

    std::string subStr = sentence.substr(sentence.find_first_not_of(whiteSpaces, sentence.find_first_of(whiteSpaces, sentence.find_first_not_of(whiteSpaces))));

    std::cout << subStr;

    std::cin.ignore();
}

0

这里是如何使用 stringstream 来提取垃圾词,同时忽略前后的任何空格(使用 std::ws),然后获取剩余的句子,并具备健壮的错误处理...

std::string sentence{"Hello how are you."};
std::stringstream ss{sentence};
std::string junkWord;
if (ss >> junkWord >> std::ws && std::getline(ss, sentence, '\0'))
    std::cout << sentence << '\n';
else
    std::cerr << "the sentence didn't contain ANY words at all\n";

点击此处在ideone上查看运行结果...


0
#include <iostream>  // cout
#include <string>   // string
#include <sstream> // string stream
using namespace std;

int main()
{
     string testString = "Hello how are you.";

     istringstream iss(testString); // note istringstream NOT sstringstream

     char c; // this will read the delima (space in this case)
     string firstWord;

     iss>>firstWord>>c; // read the first word and end after the first ' '

    cout << "The first word in \"" << testString << "\" is \"" << firstWord << "\""<<endl;

    cout << "The rest of the words is \"" <<testString.substr(firstWord.length()+1) << "\""<<endl;
    return 0;
}

输出

"Hello how are you."中的第一个单词是"Hello"
其余的单词是"how are you."

在Ideon上进行实时测试


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接