TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ String insert() function

C++ String insert() function tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ String insert()

This function is used to insert a new character, before the character indicated by the position pos.

Syntax

Consider two strings str1 and str2, pos is the positon. Syntax would be :

str1.insert(pos,str2);

Parameters

str : String object to be inserted in another string object.

pos : It defines the position at which new content is inserted just before the specified position.

subpos : It defines the position of first character in string str which is to be inserted in another string object.

sublen : It defines the number of characters of string str to be inserted in another string object.

n : It determines the number of characters to be inserted.

c : Character value to insert.

Example 1

Let's see the simple example.

     #include<iostream>
     using namespace std;
     int main()
    {
     string str1= "javat tutorial";
     cout<<"String contains :" <<str1<<'\n';
     cout<<"After insertion, String value is :"<<str1.insert(5,"point");
     return 0;
     } 

Output:

String contains : javat tutorial
After insertion, String value is TheDeveloperBlog tutorial

Example 2

Let's the simple example of insertion when subpos and sublen are given.

#include<iostream>
using namespace std;
int main()
{
string str1 = "C++  is a language";
string str2 = "programming";
cout<<"String contains :" <<str1<<'\n';
cout<<"After insertion, String is :"<< str1.insert(9,str2,0,11);
return 0;
} 

Output:

String contains C++ is a language
After insertion, String is C++ is a programming language

Example 3

Let's see the simple example of insertion when number of characters to be inserted are given.

#include<iostream>
using namespace std;
int main()
{
string str = "Maths is favorite subject" ;
cout<<"String contains :"<<str<<'\n';
cout<<"After insertion, String contains :<<str.insert(9,"my",2);
return 0;
} 

Output:

String contains : Maths is favorite subject
         After insertion, String contains : Maths is my favorite subject

Next TopicC++ Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf