c++ - [C++] 将一个字符串分割成由另一个字符串中给出的符号以外的任何分隔符分隔的字符串?

标签 c++ string

编辑:这不是重复的!给出的链接中的问题是不同的。我不是用空格分隔,我是用给定字符串中字符以外的任何东西分隔。因此,其他线程中的答案不起作用。

EDIT2:请注意,我没有提供分隔符,我需要用 notOf 以外的任何东西分隔。

在我的程序中,这意味着将名为 word 的字符串分割成名为 words 的 vector 中的字符串,这些字符串由 notOf 以外的任何内容组成并由它们分隔。所以 notOfgetline 中的定界相反。

例如,"&otherwise[taken=(and)-redacted-" 应该变成"otherwise", "taken", “and”“redacted”

我已经尝试编写并想出了您在下面看到的内容,但它不起作用,而且,有人告诉我,相当丑陋。根据您的判断是否值得修复或重新编写。

#include <string>
#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    string notOf = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789$'\"";
    string word = "&otherwise[taken=(and)-redacted-";
    stringstream in(word);
    vector<string> words;
    if (word.find_first_not_of(notOf) == string::npos)
        words.push_back(word);
    else while (word.find_first_not_of(notOf) != string::npos)
    {
        int index = word.find_first_not_of(notOf);
        string tok;
        getline(in, tok, word[index]);
        if (index != 0)
            words.push_back(tok);
        word = word.substr(index);
    }
    for (vector<string>::iterator it = words.begin(); it != words.end(); ++it)
        cout << *it << endl;
    system("pause");
    return 0;
}

最佳答案

你可以试试这个:

vector<string>split(string str,string Separator)
{
    vector<string>answer;
    string temp;
    int len=str.size();
    for(int i=0;i<len;i++)
    {
        bool isSeparator=false;
        for(int j=0;j<Separator.length();j++)
        {
            if(str[i]==Separator[j]) 
              isSeparator=true;
        }
        if(!isSeparator) 
        {
            temp+=str[i];
            continue;
        }
        if(temp!="") 
          answer.push_back(temp);
        temp=""; 
    }
    if(temp!="") 
      answer.push_back(temp);
    return answer;
}

您只需要在 Separator 字符串中指定分隔符。此函数将返回分隔的字符串。在这里,分离发生在找到 Separator 之一的地方。

编辑:

如果您想根据 notOf(即 notOf)进行分区,您可以执行以下操作:

vector<string>split(string str,string Separator)
{
    vector<string>answer;string temp;
    int len=str.size();
    for(int i=0;i<len;i++)
    {
        bool isSeparator=true;
        for(int j=0;j<Separator.length();j++)
        {
            if(str[i]==Separator[j])
              isSeparator=false;
        }
        if(!isSeparator)
        {
            temp+=str[i];
            continue;
        }
        if(temp!="")
          answer.push_back(temp);
        temp="";
    }
    if(temp!="")
      answer.push_back(temp);
    return answer;
}

int main()
{
    string notOf = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789$'\"";
    string word = "&otherwise[taken=(and)-redacted-";
    vector<string>v=split(word,notOf);
    for(int i=0;i<v.size();i++)
      cout<<v[i]<<"\n";
return 0;
}

希望对您有所帮助:)

关于c++ - [C++] 将一个字符串分割成由另一个字符串中给出的符号以外的任何分隔符分隔的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27511697/

相关文章:

c++ - 使用 erase 从 std::string 从 "("到 ")"删除字符?

java - Android中如何获取String中的一些字符

c++ - libpng:写一个大于 1002px 的 png

c# - Regex.Match 不适用于查找其他文本 C# 中包含的字符串

java - android 中的 DefaultHandler 类不会从 xml 中读取整个字符串

c++ - 创建一个 .dll 文件

python - 使用 .find 在列表中搜索字符串

c++ - boost asio tcp 服务器

c++ - 来自类数组的奇怪行为

c++ - 将 Boost 序列化与 -Wall -Wextra 一起使用时的警告