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

发布于 2024-09-26 01:36:17 字数 208 浏览 2 评论 0原文

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

例如,

  • sdfsdfds sdfs dfsd fgsd 3 dsfds 应生成:3 dsfds
  • sdfsd (dfgdg)gfdg fg 6 gg 应生成:6 gg

I have any sequence (or sentence) and i want to extract the last 2 strings.

For example,

  • sdfsdfds sdfs dfsd fgsd 3 dsfds should produce: 3 dsfds
  • sdfsd (dfgdg)gfdg fg 6 gg should produce: 6 gg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

友谊不毕业 2024-10-03 01:36:17

您可以使用 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;
}

You can use std::string::find_last_of function to find spaces.

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;
}
情泪▽动烟 2024-10-03 01:36:17

如果您的字符串以空格分隔,则以下内容将起作用。

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

The following will work if your strings are whitespace separated.

#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;
}
寄风 2024-10-03 01:36:17

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

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

Returns the strings in the wrong order, but if that doesn't matter,

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

Alternatively,

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;
}
陌路黄昏 2024-10-03 01:36:17

我鼓励您查看 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;
}

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

I would encourage you to have a look at the Boost library. It has algorithms and data structures that help you tremendously. Here's how to solve your problem using 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;
}

I would also encourage you to always use the vector::at method instead of []. This will give you proper error handling.

渔村楼浪 2024-10-03 01:36:17
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;
 }

更简单:)

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

Simpler :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文