The method insert() inserts a string at a certain position of another string.
The position is passed as the first argument.
The first character in a string occupies position 0, the second character position 1, and so on.
string s1("Miss Summer"); s1.insert(5, "test ");
The string "test "is inserted into the string s1 at position 5, that is in front of the 'S'character in "Summer".
To insert part of a string into another string, pass two additional arguments to the insert() method, the starting position and the length of the string.
string s1("this is a test"), s2("1234567890"); s1.insert(12, s2, 0, 12);
This example inserts the first 12 characters from the string s2 at position 13 in string s1.
#include <iostream> #include <string> using namespace std; int main()/*from www. j a va 2 s . c om*/ { string s1("Miss Summer"); s1.insert(5, "test "); // Insert at position: 5 cout << s1 << endl; s1 = "this is a test"; string s2("1234567890"); s1.insert(12, s2, 0, 12); cout << s1 << endl; return 0; }