how concatenate strings

i have 2 strings:

1
2
string1= "hello world";
string2 = "hello";

imagine that i need add the string2 in middle(yes i know the position) of string1... how can i do it?
It's easy if both strings are std::string objects.
std::string MyString = string1 + string2;

or if you wish to append second into first one:
string1.append(string2);
but my problem is add inside\middle of string1
add the string2 in middle of string1

I believe that is called "insertion". See http://www.cplusplus.com/reference/string/string/insert/
Sample code for insert:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
	std::string string1 = "hello world";
	std::string string2 = "hello2";

	const size_t pos = string1.find("world");
	string1.insert(pos, string2 + " ");

	std::cout << string1 << std::endl;
	std::cin.get();
	return 0;
}

Last edited on
thank you so much for all... it's working like i need
Topic archived. No new replies allowed.