Insert Substring in C++

How can I insert a substring into a specific position within a std::string?

Inserting a substring into a specific position within a std::string is done using the insert() method. This method modifies the original string by adding the new substring at the specified position.

Basic Usage

The insert() method requires two arguments: the position to insert the substring and the substring itself. For example:

#include <iostream>
#include <string>

int main(){
  std::string Greeting{"Hello!"};
  Greeting.insert(5, " World");
  std::cout << Greeting;
}
Hello World!

Inserting a Character

You can also insert a single character at a specific position.

#include <iostream>
#include <string>

int main(){
  std::string Greeting{"Hell World!"};
  Greeting.insert(4, "o");
  std::cout << Greeting;
}
Hello World!

Inserting Multiple Characters

The insert() method can also insert multiple characters at once.

#include <iostream>
#include <string>

int main(){
  std::string Greeting{"Hello!"};
  Greeting.insert(5, "!!!", 2);
  std::cout << Greeting;
}
Hello!!!

Considerations

  • Position Index: Ensure the position index is within the bounds of the string to avoid undefined behavior.
  • Efficiency: Frequent insertions can lead to performance issues due to potential reallocations.

Advanced Usage

You can also insert a substring from another string by specifying the starting position and length.

#include <iostream>
#include <string>

int main(){
  std::string Greeting{"Hello "};
  std::string Name{"Alice Wonderland"};

  Greeting.insert(6, Name, 0, 5);
  std::cout << Greeting;
}
Hello Alice

In summary, the insert() method is versatile and allows you to add substrings, single characters, or multiple characters to a std::string at any position you need.

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++?
Replace Substring in C++
How can I replace a substring within a std::string with another 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