如何从字符串中删除所有子字符串

13

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

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

3
你有尝试过什么吗? - Srinu Chinka
1
C++标准库现在包括正则表达式,其中包括替换匹配项(以及删除、替换为空字符串)的功能。 - Some programmer dude
删除所有子字符串:str.clear() - sehe
1
boost::algorithm::erase_all(str, pattern); => boost::algorithm::erase_all(str, pattern); - aahzbg
5个回答

17

从字符串中删除所有匹配的模式。

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

1
这个不能合理地处理重叠的模式。 - Cheers and hth. - Alf
@Cheersandhth.-Alf,你所说的重叠模式是什么意思? - Abhinav Gauniyal
@AbhinavGauniyal:从“shahaha”中删除所有“aha”的实例。我认为“shha”和“shah”都应该被删除。 - Cheers and hth. - Alf
1
removeSubstrs需要检查'string &p'是否为空。 - tensor5375

12

这是一个基础问题,你最好查看标准库中的字符串(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;
}

示例

正则表达式解决方案

自C++11起,您可以使用基于正则表达式的另一种解决方案(感谢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, "");
}

示例


3
尝试以下方法:

试试这样做:

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

来自将字符串的一部分替换为另一个字符串


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;

0

这是更好理解的基本逻辑 以下是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

例子: 输入-shahaha 输出-shha


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接