Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(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;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict