How to find and replace string?

后端 未结 10 1035
感动是毒
感动是毒 2020-11-27 03:51

If s is a std::string, then is there a function like the following?

s.replace(\"text to replace\", \"new text\");
相关标签:
10条回答
  • 2020-11-27 03:58
    void replace(char *str, char *strFnd, char *strRep)
    {
        for (int i = 0; i < strlen(str); i++)
        {
            int npos = -1, j, k;
            if (str[i] == strFnd[0])
            {
                for (j = 1, k = i+1; j < strlen(strFnd); j++)
                    if (str[k++] != strFnd[j])
                        break;
                npos = i;
            }
            if (npos != -1)
                for (j = 0, k = npos; j < strlen(strRep); j++)
                    str[k++] = strRep[j];
        }
    
    }
    
    int main()
    {
        char pst1[] = "There is a wrong message";
        char pfnd[] = "wrong";
        char prep[] = "right";
    
        cout << "\nintial:" << pst1;
    
        replace(pst1, pfnd, prep);
    
        cout << "\nfinal : " << pst1;
        return 0;
    }
    
    0 讨论(0)
  • #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
        string str("one three two four");
        string str2("three");
        str.replace(str.find(str2),str2.length(),"five");
        cout << str << endl;
        return 0;
    }
    

    Output

    one five two four
    
    0 讨论(0)
  • 2020-11-27 04:02
    // replaced text will be in buffer.
    void Replace(char* buffer, const char* source, const char* oldStr,  const char* newStr)
    {
        if(buffer==NULL || source == NULL || oldStr == NULL || newStr == NULL) return; 
    
        int slen = strlen(source);
        int olen = strlen(oldStr);
        int nlen = strlen(newStr);
    
        if(olen>slen) return;
        int ix=0;
    
        for(int i=0;i<slen;i++)
        {
            if(oldStr[0] == source[i])
            {
                bool found = true;
                for(int j=1;j<olen;j++)
                {
                    if(source[i+j]!=oldStr[j])
                    {
                        found = false;
                        break;
                    }
                }
    
                if(found)
                {
                    for(int j=0;j<nlen;j++)
                        buffer[ix++] = newStr[j];
    
                    i+=(olen-1);
                }
                else
                {
                    buffer[ix++] = source[i];
                }
            }
            else
            {
                buffer[ix++] = source[i];
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:05

    Do we really need a Boost library for seemingly such a simple task?

    To replace all occurences of a substring use this function:

    std::string ReplaceString(std::string subject, const std::string& search,
                              const std::string& replace) {
        size_t pos = 0;
        while ((pos = subject.find(search, pos)) != std::string::npos) {
             subject.replace(pos, search.length(), replace);
             pos += replace.length();
        }
        return subject;
    }
    

    If you need performance, here is an optimized function that modifies the input string, it does not create a copy of the string:

    void ReplaceStringInPlace(std::string& subject, const std::string& search,
                              const std::string& replace) {
        size_t pos = 0;
        while ((pos = subject.find(search, pos)) != std::string::npos) {
             subject.replace(pos, search.length(), replace);
             pos += replace.length();
        }
    }
    

    Tests:

    std::string input = "abc abc def";
    std::cout << "Input string: " << input << std::endl;
    
    std::cout << "ReplaceString() return value: " 
              << ReplaceString(input, "bc", "!!") << std::endl;
    std::cout << "ReplaceString() input string not modified: " 
              << input << std::endl;
    
    ReplaceStringInPlace(input, "bc", "??");
    std::cout << "ReplaceStringInPlace() input string modified: " 
              << input << std::endl;
    

    Output:

    Input string: abc abc def
    ReplaceString() return value: a!! a!! def
    ReplaceString() input string not modified: abc abc def
    ReplaceStringInPlace() input string modified: a?? a?? def
    
    0 讨论(0)
  • 2020-11-27 04:09

    like some say boost::replace_all

    here a dummy example:

        #include <boost/algorithm/string/replace.hpp>
    
        std::string path("file.gz");
        boost::replace_all(path, ".gz", ".zip");
    
    0 讨论(0)
  • 2020-11-27 04:11
    void replaceAll(std::string & data, const std::string &toSearch, const std::string &replaceStr)
    {
        // Get the first occurrence
        size_t pos = data.find(toSearch);
        // Repeat till end is reached
        while( pos != std::string::npos)
        {
            // Replace this occurrence of Sub String
            data.replace(pos, toSearch.size(), replaceStr);
            // Get the next occurrence from the current position
            pos =data.find(toSearch, pos + replaceStr.size());
        }
    }
    

    More CPP utilities: https://github.com/Heyshubham/CPP-Utitlities/blob/master/src/MEString.cpp#L60

    0 讨论(0)
提交回复
热议问题