2014-10-01 105 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

相關問題