Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) ReplaceAll

 

ReplaceAll is a std::string replace code snippet to replace a substring by another in a certain std::string.

 

There are multiple versions of ReplaceAll:

 

 

Below the code snippets a program that tests the ReplaceAll code snippets can be viewed and downloaded.

 

 

 

 

 

Boost ReplaceAll #1: uses Boost Strings Algorithm Library

 

#include <string>
#include <boost/algorithm/string/replace.hpp>

///Replaces all occurences of a substring
//From http://www.richelbilderbeek.nl/CppReplaceAll.htm
const std::string ReplaceAll(
  std::string s,
  const std::string& before,
  const std::string& after)
{
  return boost::algorithm::replace_all_copy(s,before,after);
}

 

 

 

 

 

Qt ReplaceAll #2: uses Qt library

 

#include <string>
#include <QString>

///Replaces all occurences of a substring
//From http://www.richelbilderbeek.nl/CppReplaceAll.htm
const std::string ReplaceAll(
  std::string s,
  const std::string& before,
  const std::string& after)
{
  QString q(s.c_str());
  q.replace(before.c_str(),after.c_str());
  return q.toStdString();
}

 

 

 

 

 

Stl ReplaceAll #3: uses STL only

 

#include <string>

///Replaces all occurences of a substring
//From http://www.richelbilderbeek.nl/CppReplaceAll.htm
const std::string ReplaceAll(
  std::string s,
  const std::string& before,
  const std::string& after)
{
  while(1)
  {
    const int pos = s.find(before);
    if (pos==-1) break;
    s.replace(pos,before.size(),after);
  }
  return s;
}

 

 

 

 

 

Full testing code

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppReplaceAll.pro

 

TEMPLATE = app
CONFIG += console
CONFIG += qt
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <string>
#include <boost/algorithm/string/replace.hpp>

///Replaces all occurences of a substring
//From http://www.richelbilderbeek.nl/CppReplaceAll.htm
const std::string ReplaceAllBoost(
  std::string s,
  const std::string& before,
  const std::string& after)
{
  return boost::algorithm::replace_all_copy(s,before,after);
}

#include <QString>

///Replaces all occurences of a substring
//From http://www.richelbilderbeek.nl/CppReplaceAll.htm
const std::string ReplaceAllQt(
  std::string s,
  const std::string& before,
  const std::string& after)
{
  QString q(s.c_str());
  q.replace(before.c_str(),after.c_str());
  return q.toStdString();
}

///Replaces all occurences of a substring
//From http://www.richelbilderbeek.nl/CppReplaceAll.htm
const std::string ReplaceAllStl(
  std::string s,
  const std::string& before,
  const std::string& after)
{
  while(1)
  {
    const int pos = s.find(before);
    if (pos==-1) break;
    s.replace(pos,before.size(),after);
  }
  return s;
}

#include <cassert>

int main()
{
  //Test single char replacement
  {
    //s: input string
    //x: expected output string
    //b: before
    //a: after
    const std::string s = "abcdbef";
    const std::string x = "a_cd_ef";
    const std::string b = "b";
    const std::string a = "_";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
  //Test single char to multiple chars replacement
  {
    const std::string s = "abcdbef";
    const std::string x = "a__cd__ef";
    const std::string b = "b";
    const std::string a = "__";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
  //Test single char to no char replacement
  {
    const std::string s = "abcdbef";
    const std::string x = "acdef";
    const std::string b = "b";
    const std::string a = "";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
  //Test multiple chars to multiple chars replacement
  {
    const std::string s = "abcdbcef";
    const std::string x = "a__d__ef";
    const std::string b = "bc";
    const std::string a = "__";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
  //Test multiple chars to more chars replacement
  {
    const std::string s = "abcdbcef";
    const std::string x = "a___d___ef";
    const std::string b = "bc";
    const std::string a = "___";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
  //Test multiple chars to single char replacement
  {
    const std::string s = "abcdbcef";
    const std::string x = "a_d_ef";
    const std::string b = "bc";
    const std::string a = "_";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
  //Test multiple chars to no char replacement
  {
    const std::string s = "abcdbcef";
    const std::string x = "adef";
    const std::string b = "bc";
    const std::string a = "";
    assert(ReplaceAllBoost(s,b,a) == x);
    assert(ReplaceAllQt(s,b,a) == x);
    assert(ReplaceAllStl(s,b,a) == x);
  }
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml