C++ string Insert a substring at insertion point
#include <cstdlib> #include <cstdio> #include <iostream> using namespace std; string insertPhrase(const string& source) { string s = source;//from w ww . j a va 2 s .c o m size_t offset = s.find("<ip>"); if (offset != string::npos) { s.erase(offset, 4); s.insert(offset, "Randall"); } return s; } int main(int argc, char* pArgs[]) { string s3 = "Stephen <ip> Davis"; cout << s3 + " -> " + insertPhrase(s3) << endl; return 0; }