C++ tutorial-insert function


C++ String Manipulation

C++ insert() function

By using insert() function you can insert a string or a proportion of a string to another string in a position that you want. The insert() function has two forms. The first form inserts all characters of a string to another string in the position specified by the position argument. The second form inserts a proportion of a string to another string.

insert(position, string);

insert(position,string, start, num_chars);



Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>


using namespace std;


void strmanip(){
  string str="C++ programming";
  str.insert(3," Java");//insert string " Java" to string str at the position 3
  cout<<str<<endl;
  str.insert(3," C# programming",0,3);//insert string " C#" to string str at the position 3
  cout<<str<<endl;

}

int main(){

  strmanip();
  getch();
  return 0;

}


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.