简体   繁体   English

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

[英]How to remove all substrings from a string

How to remove all instances of the pattern from a string?如何从字符串中删除模式的所有实例?

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

Removes all instances of the pattern from a string,从字符串中删除模式的所有实例,

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

This is a basic question and you'd better take a look at the string capabilities in the standard library.这是一个基本问题,您最好查看标准库中的字符串功能。

Classic solution经典方案

#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例子

RegEx solution正则表达式解决方案

Since C++11 you have another solution (thanks Joachim for reminding me of this) based on regular expressions从 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, "");
}

Example例子

Try something like:尝试类似:

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

From Replace part of a string with another string From 用另一个字符串替换部分字符串

This is the basic logic for better understanding Here is the code in c++这是更好理解的基本逻辑这里是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

example: input-shahaha output-shha示例:输入-shahaha 输出-shha

A faster algorithm building the string one character at a time and checking if the end matches the substring.一种更快的算法,一次构建一个字符的字符串并检查结尾是否与子字符串匹配。 The following runs in o(substring * string) where as the above solutions run in o(s^2/t).以下在 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM