Thursday, December 8, 2022

Replace string in C++ using replace() or regex_replace()

Everything you need to know about replacing a string in C++

Replace string using replace() function

To replace a string in C++, you can use the replace() function from the string library. This function takes three arguments: the position at which to start replacing, the number of characters to replace, and the string to insert in place of the characters that are being replaced. Here is an example:


  #include 
  #include 

  using namespace std;

  int main() {
    string str = "Hello, world!";
    str.replace(7, 5, "Earth");

    cout << str << endl;  // Outputs "Hello, Earth!"

    return 0;
  }

In this example, the replace() function is used to replace the string "world" with "Earth" in the str string. The replace() function is called on the str string and is passed the following arguments:

  • 7: the position at which to start replacing characters, which is the index of the 'w' character in the str string
  • 5: the number of characters to replace, which is the length of the string "world"
  • "Earth": the string to insert in place of the characters being replaced

After the call to replace(), the str string is printed to the standard output stream and will contain the string "Hello, Earth!" instead of "Hello, world!".

Replace string using regular expression with the regex_replace() function

To replace a string using a regular expression (regex) in C++, you can use the regex_replace() function from the regex library. This function takes the input string, the regular expression to match, and the string to insert in place of the matched regex pattern. Here is an example:


  #include 
  #include 
  #include 

  using namespace std;

  int main() {
    string str = "Hello, world!";
    regex pattern("world");
    str = regex_replace(str, pattern, "Earth");

    cout << str << endl;  // Outputs "Hello, Earth!"

    return 0;
  }

In this example, the regex_replace() function is used to replace the string "world" with "Earth" in the str string. The regex_replace() function is called and is passed the following arguments:

  • str: the input string
  • pattern: the regex pattern to match, which is the string "world" wrapped in a regex object
  • "Earth": the string to insert in place of the matched regex pattern

After the call to regex_replace(), the str string is assigned the modified string and is printed to the standard output stream, which will contain the string "Hello, Earth!" instead of "Hello, world!".

Advanced example of replacing only numbers from a string with regular expression

To replace numbers with a dash character (-) in C++ using regex, you can use the regex_replace() function from the regex library. This function takes the input string, the regex pattern to match, and the string to insert in place of the matched regex pattern. The regex pattern to match numbers can be defined using the \d character class, which matches any digit from 0 to 9. Here is an example:


  #include 
  #include 
  #include 

  using namespace std;

  int main() {
    string str = "The 1st number is 4 and the 2nd number is 6";
    regex pattern("\\d");
    str = regex_replace(str, pattern, "-");

    cout << str << endl;  // Outputs "The -st number is - and the -nd number is -"

    return 0;
  }

In this example, the regex_replace() function is used to replace all the numbers in the str string with a dash character (-). The regex_replace() function is called and is passed the following arguments:

  • str: the input string
  • pattern: the regex pattern to match, which is the \d character class wrapped in a regex object. This pattern matches any digit from 0 to 9.
  • "-": the string to insert in place of the matched regex pattern, which is a single dash character in this case

After the call to regex_replace(), the str string is assigned the modified string and is printed to the standard output stream, which will contain the string "The -st number is - and the -nd number is -" with all the numbers replaced by a dash character.

Popular Posts