JAN
JAN

Reputation: 21865

How to place a string inside another string , instead of the character at some index

If I have : Enter(x)

And I want to place after the ( the word room2 , but without x , then std::insert doesn't help since if :

std::string myString = "Enter(x)";
std::string placeMe = "room2";
myString.insert(myString.find_first_of("(") + 1 , placeMe);

Then I'd get : Enter(room2x) .

How can I place the placeMe instead of a certain index ?

Upvotes: 0

Views: 150

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

First, you probably want find instead of find_first_of, since you only want to match one possible substring. Second, you want the std::string::replace function:

myString.replace(myString.find("(") + 1, 1, placeMe);

Upvotes: 3

zer0bit
zer0bit

Reputation: 615

I believe the function you are looking for is replace instead of insert.

http://www.cplusplus.com/reference/string/string/replace/

Upvotes: 6

Related Questions