How can I use insert() method of std::string in my C++ app? How can I insert a string into another string? Can we insert a char array into a string? How can I insert number of chars into a string? Is it possible to insert a substring of a string into another string? Here are the answers with C++ examples.
Table of Contents
What is a string in a C++ app?
In a C++, app in addition to int, float, double there is another data type, string, that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types are defined in header <string>.
strings are the string class for byte characters represented with the keyword string
. Alphanumeric characters are stored and displayed in string
forms. In other words, string stores alphanumeric text with 1-byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. We can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
string
has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can insert a string to another string in different methods. Let’s see some of most used methods.
How to insert a string into another string in C++ with the insert() method?
insert() method of std::string, inserts the characters of string in the given range at the position index
. We can use the insert() method without the range parameter, just give the insert position index (insert_position, where to insert) and the string to be inserted (insert_str). Here is the syntax of the insert() method,
1 2 3 |
basic_string& insert( size_type insert_position, const basic_string& insert_str ); // Until C++20 |
Here is an example of how we can use insert() method in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first string "; s2 = "and the second "; s1.insert( 6, s2 ); // Inserts s2 to the position 6 of the s1 std::cout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
first and the second string |
How to insert a string from a starting index into another string at a specific position with the C++ insert() method
insert() method of std::string, inserts the characters of string in the given range at the position index
. We can use insert() method without the range parameter, just give the insert position (insert_position, where to insert,) and the string (s) to be inserted then where to start (start_from) to be copied from. Here is syntax of a insert() method that can be used with a starting index (start_from),
1 2 3 |
basic_string& insert( size_type insert_position, const basic_string& insert_str, size_type start_from ); // Until C++20 |
Here is a example how we can use insert() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first string "; s2 = "and the insert"; s1.insert( 5, s2, 3 ); // Inserts s2 starting from 3rd char to the position 5 of the s1 std::cout << s1 << std::endl; string_insert_atposition_inrange(); getchar(); return 0; } |
and the output will be,
1 2 3 |
first the insert string |
How to insert a String using a range into another String at a specific position with the C++ app insert() method
insert() method of std::string, inserts the characters of string in the given range at the position index
. We can use insert() method without the range parameter, just give the insert position (insert_position, where to insert,) and the string (s) to be inserted then where to start (start_from) to be copied from. Here is syntax of a insert() method that can be used with a starting index (start_from),
1 2 3 |
basic_string& insert( size_type insert_position, const basic_string& insert_str, size_type start_from, size_type numof_chars); // Until C++20 |
Here is a example how we can use insert() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first string "; s2 = "and the insert"; s1.insert( 5, s2, 3, 4 ); // Inserts 4 chars of s2 starting from 3rd char to the position 5 of the s1 std::cout << s1 << std::endl; string_insert_atposition_inrange(); getchar(); return 0; } |
1 2 3 |
first the string |
There are many other ways to use insert methods(). Note that we can also use these insert() methods to append wstring in C++.
Do you want to try out these examples yourself in your own C++ app? Why not download a free trial of C++ Builder today?
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition