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

时间:2014-10-01 09:24:19

标签: c++ string stringstream strtok

假设我有

string sentence{"Hello how are you."}

如果没有“Hello”,我希望字符串句子“你好吗”。我该怎么做呢。

我尝试过这样的事情:

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

但是当我这样做的时候:

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

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

8 个答案:

答案 0 :(得分:6)

尝试以下

#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."

答案 1 :(得分:3)

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)将获取从位置开始直到字符串的最后一个字符的子字符串。

答案 2 :(得分: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,这意味着找不到空间。为清楚起见,我的示例代码中省略了该检查:)

答案 3 :(得分:0)

您可以使用string::find()找到第一个空格。获得索引后,从空格索引到字符串结尾之后,从索引中获取带有string::substr()的子字符串。

答案 4 :(得分:0)

您可以采用剩余的子字符串

string sentence{"Hello how are you."};
stringstream ss{sentence};
string junkWord;
ss >> junkWord;
cout<<sentence.substr(junkWord.length()+1); //string::substr

但是,这也取决于你想要做什么

答案 5 :(得分: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();
}

答案 6 :(得分: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";

看到它正在运行on ideone here ....

答案 7 :(得分:0)

https://www.formatmyharddrive.com/?confirm=yesofcourse

输出

The first word in "Hello how are you." is "Hello"
The rest of the words is "how are you."

live testing at ideon