In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Geeks";
// Inserting another string at the friend
// of s
s.insert(s.size(), "forGeeks");
cout << s;
return 0;
}
The string method is a member function of std::string class defined inside <string> header file. It is implemented in five forms:
C++
str.insert(pos, c);
str.insert(pos, num, c);
str.insert(pos, first, last);
str1.insert(pos, str2);
str1.insert(pos, str2, str_idx, str_num);
These 5 implementation can be used in the following ways:
Insert a Single Character
The string insert() method can be used to insert a single character at a given position of the string.
Syntax
C++
Parameters:
- str: String in which we have to insert the string.
- pos: Iterator pointing to the position where we have to insert the character.
- c: Character which we have to insert.
Return Value:
- It will return an iterator pointing to the inserted character of the string.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string str("Geeksforeeks");
// Inserts 'G' at position
// str.begin() + 8
str.insert(str.begin() + 8, 'G');
cout << str;
return 0;
}
If the character is inserted in the middle of the string, all the characters to the right of the given index will be shifted one place to the right to make space for new characters.
Insert a Single Character Multiple Times
The string insert() method can also be used to insert a single character multiple time at the given position in the string.
Syntax
C++
Parameter:
- pos: Iterator or index to the position where we have to insert.
- num: Number of times we have to insert the character.
- c: Character which we have to insert.
Return Value:
- It will return the original string after inserting the characters.
Example
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string str("GeeksforGks");
// Insert 2 occurrences of 'e'
// starting from position 9
str.insert(9, 2, 'e');
cout << str;
return 0;
}
Insert Characters from the Given Range
We can insert the characters from the given range to our string using string insert() method. This range can be any STL container.
Syntax
C++
str.insert(pos, first, last);
Parameter:
- pos: Iterator or index to the position where we have to insert.
- first: Iterator pointing to the first character in the range.
- last: Iterator pointing to the character just after the last character in the range.
Return Value:
- It will return an iterator pointing to the first inserted character.
Example
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1("GeeksGeeks");
string str2("GforG");
// Defining the range as str2.begin()
// + 1 to str2.end() - 1
auto first = str2.begin() + 1;
auto last = str2.end() - 1;
// Inserts at position
// str1.begin() + 5
str1.insert(str1.begin() + 5, first, last);
cout << str1;
return 0;
}
Insert a String
The string insert() method is used to insert the string at the specific position of the string. All the characters to the right of the given index will be shifted right to make space for new characters.
Syntax
C++
Parameter:
- str1: It is the name of the string in which we have to insert the string.
- pos: It is the position where we have to insert.
- str2: It is the name of the string from which we have to insert.
Return Value:
- It will return the original string after inserting the string.
Example
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1("Hello World! ");
string str2("GeeksforGeeks ");
// Inserts str2 in str1 starting
// from 6th index of str1
str1.insert(6, str2);
cout << str1;
return 0;
}
OutputHello GeeksforGeeks World!
Insert a Part of the String
The string insert() method can be used to insert the substring at any particular position.
Syntax
C++
str1.insert(pos, str2, str_idx, str_num);
Parameter:
- str1: It is the name of the string in which we have to insert the string.
- pos: It is the position where we have to insert.
- str2: It is the name of the string from which we have to insert.
- str_idx: It is the starting position of the string from which we have to insert.
- str_num: It represent the length of the substring which we have to insert.
Return Value:
- It will return the original string after inserting the string.
Example
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1("Hello World! ");
string str2("GeeksforGeeks ");
// Inserts 6 characters from
// index number 8 of str2 at
// index number 6 of str1
str1.insert(6, str2, 8, 6);
cout << str1;
return 0;
}
Similar Reads
std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
String find() in C++ In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Welcome to GfG!"; s
4 min read
std::string::size() in C++ The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size()
2 min read
std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin
1 min read
getline (string) in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It extracts the characters from the input stream and appends it to the given string object until the delimiting character is encountered.Example:C++#include <bits/stdc++.h> using name
3 min read
std::string::resize() in C++ resize() lets you change the number of characters. Here we will describe two syntaxes supported by std::string::resize() in C++ Return Value : None Syntax 1: Resize the number of characters of *this to num. void string ::resize (size_type num) num: New string length, expressed in number of character
3 min read
std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po
2 min read
Strings in C++ In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s
5 min read
Raw String Literal in C++ A Literal is a constant variable whose value does not change during the lifetime of the program. Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \" ' of C++ are not processed. Hence, a raw string literal that starts with R"( and ends in )". The syntax for R
2 min read
Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read