2010-10-06 77 views
2

我有任何序列(或句子),我想提取最後2個字符串。從字符串序列中提取最後2個字,空格分隔

例如,

  • sdfsdfds sdfs dfsd fgsd 3 dsfds應出示:3 dsfds
  • sdfsd (dfgdg)gfdg fg 6 gg應出示:6 gg
+0

也看到這個問題:http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c。此外,這與STL(這是處理容器,迭代器和算法的std庫的一部分)無關,所以我更改了標記。 – sbi 2010-10-06 18:22:27

+0

@sbi:std :: string及其查找方法(rfind在這種情況下很有用)是STL的一部分。所以我寧願不刪除STL標籤。 – 2010-10-06 18:28:59

+0

@Vlad:「STL」是來自原始STL的標準庫的那些部分的口語名稱。 [它不是標準庫的同義詞]。(http://en.wikipedia.org/wiki/Standard_Template_Library)有人可能會爭論'std :: string'(在問題中甚至沒有提到),這是在草案標準很長一段時間STL來了,只是後來「STLified」,屬於STL。它無數的成員函數絕對不會。 (算法與容器是分開的這一事實,畢竟STL是分開的。)我不會爭奪那個標籤,但這是錯誤的。 – sbi 2010-10-08 16:21:49

回答

4

您可以使用std::string::find_last_of函數查找空格。

int main() 
{ 
    std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds"; 

    size_t found1 = test.find_last_of(" "); 
    if (found1 != string::npos) { 
     size_t found2 = test.find_last_of(" ", found1-1); 
     if (found2 != string::npos) 
      std::cout << test.substr(found2+1, found1-found2-1) << std::endl; 
     std::cout << test.substr(found1+1) << std::endl; 
    } 

    return 0; 
} 
2

下面的工作,如果你的字符串空格隔開。

#include <iostream> 
#include <string> 
#include <sstream> 
#include <vector> 
using namespace std; 

int main() 
{ 
    string str = "jfdf fhfeif shfowejef dhfojfe"; 
    stringstream sstr(str); 
    vector<string> vstr; 

    while(sstr >> str) 
    { 
     vstr.push_back(str); 
    } 

    if (vstr.size() >= 2) 
     cout << vstr[vstr.size()-2] << ' '; 
    if (vstr.size()) 
     cout << vstr[vstr.size()-1] << endl; 

    return 0; 
} 
+0

在訪問它之前,您可能需要檢查向量的大小是否大於2。 – sbi 2010-10-06 18:26:15

+0

是的,謝謝。我認爲會有足夠的字符串提取。 – Donotalo 2010-10-06 18:28:13

+0

@Donato :(你需要正確的@地址評論回覆讓他們出現在其他人的回覆標籤中,我只是偶然來到這裏)。現在就投了票。 – sbi 2010-10-08 16:14:38

1

返回錯誤的順序串,但如果沒有關係,

std::string s ("some words here"); 

std::string::size_type j; 
for(int i=0; i<2; ++i) { 
    if((j = s.find_last_of(' ')) == std::string::npos) { 
     // there aren't two strings, throw, return, or do something else 
     return 0; 
    } 
    std::cout << s.c_str()+j+1; 
    s = " " + s.substr(0,j); 
} 

另外,

struct extract_two_words { 
    friend std::istream& operator>> (std::istream& in , extract_two_words& etw); 
    std::string word1; 
    std::string word2; 
}; 

std::istream& operator>> (std::istream& in , extract_two_words& etw) { 
    std::string str1, str2; 
    while(in) { 
     in >> str1; 
     in >> str2; 
    } 
    etw.word2 = str1; 
    etw.word1 = str2; 
} 
1

我會鼓勵你看看了Boost圖書館。它有算法和數據結構,可以幫助您大大提高。以下是如何使用Boost.StringAlgo解決你的問題:

#include <boost/algorithm/string/split.hpp> 
#include <iostream> 
#include <vector> 
#include <string> 

int main() 
{ 
    std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds"; 

    std::vector<std::string> v; 
    boost::algorithm::split(v, test, [](char c) { return c==' ';}); 
    std::cout << "Second to last: " << v.at(v.size()-2) << std::endl; 
    std::cout << "Last:   " << v.at(v.size()-1) << std::endl; 
} 

我也會鼓勵你總是使用矢量::在方法,而不是[]。這會給你適當的錯誤處理。

1
int main() 
{ 
    std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds"; 
    size_t pos = test.length(); 
    for (int i=0; i < 2; i++) 
     pos = test.find_last_of(" ", pos-1); 
    std::cout << test.substr(pos+1) << std::endl; 
} 

簡單:)

相關問題