代码之家  ›  专栏  ›  技术社区  ›  vehomzzz

从字符串序列中提取最后两个单词,空格分隔

  •  2
  • vehomzzz  · 技术社区  · 14 年前

    我有任何序列(或句子),我想提取最后两个字符串。

    例如,

    • sdfsdfds sdfs dfsd fgsd 3 dsfds 应产生: 3 dsfds
    • sdfsd (dfgdg)gfdg fg 6 gg 应产生: 6 gg
    5 回复  |  直到 14 年前
        1
  •  4
  •   Kirill V. Lyadvinsky    14 年前

    你可以用 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
  •  2
  •   Donotalo    14 年前

    如果字符串是空格分隔的,则以下操作将起作用。

    #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;
    }
    
        3
  •  1
  •   tjm    14 年前

    以错误的顺序返回字符串,但如果这不重要,

    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;
    }
    
        4
  •  1
  •   Daniel Lidström    14 年前

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

    我也鼓励你 总是 使用vector::at方法而不是[]。这将为您提供正确的错误处理。

        5
  •  1
  •   dr.manhattan    14 年前
    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;
     }
    

    更简单:)