How to find and replace a string?

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

 s.replace("text to replace", "new text"); 
+48
c ++ string
May 4 '11 at 4:58
source share
9 answers

Try a combination of std::string::find and std::string::replace .

This gets the position:

 std::string s; std::string toReplace("text to replace"); size_t pos = s.find(toReplace); 

And this replaces the first appearance:

 s.replace(pos, toReplace.length(), "new text"); 



Now you can simply create a function for your convenience:

 std::string replaceFirstOccurrence( std::string& s, const std::string& toReplace, const std::string& replaceWith) { std::size_t pos = s.find(toReplace); if (pos == std::string::npos) return s; return s.replace(pos, toReplace.length(), replaceWith); } 
+75
May 04 '11 at 5:01
source share

Yes: replace_all is one of the forced string algorithms:

Although this is not a standard library, there are several things in the standard library:

  • A more natural notation based on ranges rather than iterative pairs. This is good because you can nest string manipulations (e.g. replace_all nested inside trim ). This is a bit more important for standard library functions.
  • Completeness. It is not difficult to be "better"; The standard library is pretty Spartan. For example, forced string algorithms give you explicit control over how string manipulations are performed (i.e., in-place or through a copy).
+23
May 04 '11 at 5:01
source share

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

To replace all occurrences 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 
+23
Feb 04 '13 at 0:26
source share
 #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; } 

Exit

 one five two four 
+10
May 04 '11 at 5:09
source share

as some boost :: replace_all say

here's a dummy example:

  #include <boost/algorithm/string/replace.hpp> std::string path("file.gz"); boost::replace_all(path, ".gz", ".zip"); 
+7
Jan 24 '13 at 2:32
source share

Not really, but std::string has many replace overloaded functions.

Follow this link to see an explanation of each, with examples of how they are used.

In addition, there are several versions of string::find functions (listed below) that you can use in combination with string::replace .

  • to find
  • rfind
  • find_first_of
  • find_last_of
  • find_first_not_of
  • find_last_not_of



Also note that there are several versions of replace functions available from <algorithm> that you can also use (instead of string::replace ):

  • replace
  • replace_if
  • replace_copy
  • replace_copy_if
+2
May 04 '11 at 5:01
source share
 // 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]; } } } 
+2
Oct 17 '13 at 14:25
source share

In this version, I wrote an entry that replaces all instances of the target row in the given row. Works with any type of string.

 template <typename T, typename U> T &replace ( T &str, const U &from, const U &to) { size_t pos; size_t offset = 0; const size_t increment = to.size(); while ((pos = str.find(from, offset)) != T::npos) { str.replace(pos, from.size(), to); offset = pos + increment; } return str; } 

Example:

 auto foo = "this is a test"s; replace(foo, "is"s, "wis"s); cout << foo; 

Output:

 thwis wis a test 

Note that even if the search string appears in the replacement string, this works correctly.

0
Feb 10 '16 at 0:50
source share
 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
Dec 04 '18 at 9:05
source share



All Articles