How to remove all substrings from a string

前端 未结 4 960
终归单人心
终归单人心 2020-12-01 23:56

How to remove all instances of the pattern from a string?

string str = \"red tuna, blue tuna, black tuna, one tuna\";
string pattern = \"tuna\";
4条回答
  •  心在旅途
    2020-12-02 00:52

    This is the basic logic for better understanding Here is the code in 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<

    example: input-shahaha output-shha

提交回复
热议问题