Alex Rivera | Logout

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

Asked 2011-04-09T18:50:10.197
24

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?

Edit
Report

1 Answer

34

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, "/", "\\/");
answered 2011-04-09T20:14:56.717

Your Answer