2014-10-01 106 views
3

比方说,我有如何从字符串中删除第一个单词?

string sentence{"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

+0

怎么样了分裂串入字(通过字符串流),然后readding所有单词除了第一个? – 2014-10-01 09:25:36

+0

这可能会要求我使用while循环并创建动态字符串数组。更简单的方法? – user3247278 2014-10-01 09:28:46

+1

你只需要使用一个向量,没有循环。看看这个:https://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – 2014-10-01 09:29:51

回答

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

+0

看看弗拉德的答案,看看如何解释领先的空白。 – pmr 2014-10-01 09:49:03

+0

谢谢维杰!最终每个人的回答都很重要,我非常感谢大家的帮助。维杰你的解决方案是最简单和高效的,我感谢你的帮助。你能否快速解释一下:find_first_of(“”)+1再次感谢! – user3247278 2014-10-01 09:50:13

0

您可以使用string::find()找到第一个空格。一旦你有了它的索引,然后从空间的索引后面的索引获得string::substr()的子字符串,直到字符串的末尾。

0

比如,你可以在剩下的子

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

然而,这也取决于你想进一步

+0

我宁愿再次使用字符串句子变量,不仅打印剩余的字符串。我不太熟悉substr,也不太清楚你在用junkWord.length()+ 1做什么。 – user3247278 2014-10-01 09:37:05

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,这意味着空间没有被发现。检查省略了我的示例代码为清楚起见:)

5

请尝试以下

#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." 
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)后忽略任何空间junkword,然后得到休息具有强大的错误处理功能....

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"; 

S ee值它运行on ideone here ....

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; 
} 

输出

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

live testing at ideon

相关问题