c++replaceescapingstdstring

How to replace all occurrences of one character with two characters using std::string?


Is there a nice simple way to replace all occurrences of "/" in a std::string with "\/" to escape all the slashes in a std::string?


Solution

  • Probably the simplest way to get this done is with boost string algorithms library.

      boost::replace_all(myString, "/", "\\/");
    
      std::string result = boost::replace_all_copy(myString, "/", "\\/");