【问题标题】:How to remove all substrings from a string如何从字符串中删除所有子字符串
【发布时间】:2015-12-02 19:02:03
【问题描述】:

如何从字符串中删除模式的所有实例?

string str = "red tuna, blue tuna, black tuna, one tuna";
string pattern = "tuna";

【问题讨论】:

  • 必须尝试什么?
  • C++ 标准库现在包括regular expressions,包括replace 匹配的设施(以及删除,替换为空字符串)。
  • 删除所有子字符串:str.clear()
  • boost::algorithm::erase_all(str, pattern);

标签: c++ string substring


【解决方案1】:

一种更快的算法,一次构建一个字符的字符串,并检查结尾是否与子字符串匹配。以下在 o(substring * string) 中运行,而上述解决方案在 o(s^2/t) 中运行。

string S, T;
  cin >> S >> T;

  /* Build R, the result string, one character at a time. */
  string R;
  for (int i = 0; i < S.size(); i++) {
    R += S[i];

    /* If the end of R matches T then delete it. */
    if (R.size() >= T.size() && R.substr(R.size() - T.size()) == T) {
      R.resize(R.size() - T.size());
    }
  }

  cout << R << endl;

【讨论】:

    【解决方案2】:

    这是更好理解的基本逻辑 这是c++中的代码

        string s,x;                //s is the string , x is the substring
        int a,l; 
        cin>>s>>x;
        l=x.length();
        while(true)
        {
            a=s.find(x);
            if(a==-1)
            {break;}                       // if substring is not found
            else
            {
            s.erase(a,l);                  //if substring is found 
            }
        }
        if(s.length()==0)                  // if string length becomes null printing 0 
            cout<<0<<"\n";      
        else 
            cout<<s<<endl;                 // else printing the string
    

    示例: 输入-哈哈哈 输出-shha

    【讨论】:

      【解决方案3】:

      这是一个基本问题,你最好看看标准库中的string功能。

      经典解决方案

      #include <iostream>
      #include <string>
      
      int main() { 
         std::string str = "red tuna, blue tuna, black tuna, one tuna";
         std::string pattern = "tuna";
      
         std::string::size_type i = str.find(pattern);
         while (i != std::string::npos) {
           str.erase(i, pattern.length());
           i = str.find(pattern, i);
         }
      
         std::cout << str;
      }
      

      Example

      正则表达式解决方案

      从 C++11 开始,您有了另一个基于 regular expressions 的解决方案(感谢 Joachim 提醒我)

      #include <iostream>
      #include <string>
      #include <regex>
      
      int main() { 
         std::string str = "red tuna, blue tuna, black tuna, one tuna";
         std::regex pattern("tuna");
      
         std::cout << std::regex_replace(str, pattern, "");
      }
      

      Example

      【讨论】:

        【解决方案4】:

        从字符串中删除模式的所有实例,

        #include <string>
        #include <iostream>
        
        using namespace std;
        
        void removeSubstrs(string& s, string& p) { 
          string::size_type n = p.length();
          for (string::size_type i = s.find(p);
              i != string::npos;
              i = s.find(p))
              s.erase(i, n);
        }
        
        int main() {
        
          string str = "red tuna, blue tuna, black tuna, one tuna";
          string pattern = "tuna";
        
          removeSubstrs(str, pattern);
          cout << str << endl;
        }
        

        【讨论】:

        • 这不能合理地处理重叠模式。
        • @Cheersandhth.-Alf 你所说的重叠模式是什么意思?
        • @AbhinavGauniyal:从"shahaha" 中删除"aha" 的所有实例。我认为"sh???ha""shah???" 都应该被删除。
        • removeSubstrs 需要检查 'string &p' 是否为空。
        【解决方案5】:

        尝试类似:

        void replaceAll(std::string& str, const std::string& from, const std::string& to) {
            if(from.empty())
                return;
            size_t start_pos = 0;
            while((start_pos = str.find(from, start_pos)) != std::string::npos) {
                str.replace(start_pos, from.length(), to);
                start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
            }
        }
        

        来自Replace part of a string with another string

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-04
          • 2014-08-07
          • 2021-04-07
          • 2019-09-08
          • 1970-01-01
          • 2011-10-15
          相关资源
          最近更新 更多