Site icon Learn C++

How To Insert A String Into Another String In A C++ App

How To Insert A String Into Another String In A C++ App

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.

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,

[crayon-6639024ed1acd667743663/]

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,

[crayon-6639024ed1ad5387367631/]

Here is an example of how we can use insert() method in C++

[crayon-6639024ed1ad7510130171/]

and the output will be,

[crayon-6639024ed1ad8113998003/]

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),

[crayon-6639024ed1ada740275573/]

Here is a example how we can use insert() method with in this given syntax, see example below,

[crayon-6639024ed1adc462551416/]

and the output will be,

[crayon-6639024ed1ade988105308/]

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),

[crayon-6639024ed1ae0365843089/]

Here is a example how we can use insert() method with in this given syntax, see example below,

[crayon-6639024ed1ae3467666775/]
[crayon-6639024ed1ae5677829170/]

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?

Exit mobile version