(C++) ReplaceOnce
January 7, 2018 · View on GitHub
(C++) ReplaceOnce
ReplaceOnce is a std::string replace code snippet to replace a substring by another in a certain std::string once.
Use ReplaceAll to replace all substrings in a certain std::string.
#include <string> //From http://www.richelbilderbeek.nl/CppReplaceOnce.htm const std::string ReplaceOnce( std::string s, const std::string& replaceWhat, const std::string& replaceWithWhat) { const int pos = s.find(replaceWhat); if (pos==-1) return s; s.replace(pos,replaceWhat.size(),replaceWithWhat); return s; }