Replace Substring in C++

How can I replace a substring within a std::string with another string?

Replacing a substring within a std::string with another string is accomplished using the replace() method. This method allows you to specify the starting position, the number of characters to replace, and the new substring.

Basic Usage

The replace() method requires three arguments: the starting position, the number of characters to replace, and the new substring. For example:

#include <iostream>
#include <string>

int main(){
  std::string Greeting{"Hello World!"};

  Greeting.replace(6, 5, "Everyone");

  std::cout << Greeting;
}
Hello Everyone!

Replacing with a Different Length

The new substring does not need to be the same length as the substring being replaced. The string will adjust its size accordingly.

#include <iostream>
#include <string>

int main(){
  std::string Greeting{"Hello Everyone!"};

  Greeting.replace(6, 8, "World");

  std::cout << Greeting;
}
Hello World!

Using Iterators

You can also use iterators to specify the range of characters to replace. This can be useful when working with more complex string manipulations.

#include <iostream>
#include <string>

int main() {
  std::string Greeting{"Hello World!"};
  std::string Replacement{"Everyone"};

  Greeting.replace(Greeting.begin() + 6,   
                   Greeting.begin() + 11,  
                   Replacement);  

  std::cout << Greeting;
}
Hello Everyone!

Considerations

  • Bounds Checking: Ensure the starting position and length are within the bounds of the string to avoid undefined behavior.
  • Efficiency: Large replacements can be inefficient due to potential reallocations.

Advanced Usage

For more control, you can replace a substring from another string by specifying the starting position and length within the new string.

#include <iostream>
#include <string>

int main() {
  std::string Greeting{"Hello World!"};
  std::string Replacement{"Greetings Earthlings"};

  Greeting.replace(6, 5, Replacement, 10, 5);  

  std::cout << Greeting;
}
Hello Earth!

The replace() method is powerful and flexible, making it easy to modify parts of a std::string as needed.

Manipulating std::string Objects

A practical guide covering the most useful methods and operators for working with std::string objects and their memory

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Concatenate Strings in C++
How can I concatenate two std::string objects in C++?
The append() Method vs += Operator
What is the difference between append() and += operator for strings in C++?
Insert Substring in C++
How can I insert a substring into a specific position within a std::string?
Iterate over String
How can I iterate through each character of a std::string using a range-based for loop?
Reserve vs Resize
What is the difference between reserve() and resize() methods in std::string?
Convert to wstring
How can I convert a std::string to a std::wstring?
Count Character Occurrences
How can I count the number of occurrences of a character in a std::string?
Restrict String Length
How can I ensure a std::string does not exceed a specific length?
Reverse String
How can I reverse the contents of a std::string?
Trim Whitespace
How can I trim whitespace from the beginning and end of a std::string?
Compare String vs Vector
What are the differences between std::string and std::vector?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant