TheDeveloperBlog.com

Home | Contact Us

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

C++ String append() function

C++ String append 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 append()

This function is used to extend the string by appending at the end of the current value.

Syntax

Consider string str1 and str2. Syntax would be :

Str1.append(str2);
Str1.append(str2,pos,len);
Str1.append(str2,n);

Parameters

str : String object which is to be appended in another string object.

pos : It determines the position of the first character that is to be appended to another object.

len : Number of characters to be copied in another string object as substring.

n : Number of characters to copy.

Return value

This function does not return any value.

Example 1

Let's see the example of appending the string in another string object.

#include<iostream>
using namespace std;
int main()
{
string str1="Welcome to C++ programming";
string str2="language";
cout<<"Before appending,string value is"<<str1<<'\n';
str1.append(str2);
cout<<"After appending, string value is"<<str1<<'\n';
return 0;
} 

Output:

Before appending,string value is Welcome to C++ programming
After appending,string value is Welcome to C++ programming language

Example 2

Let's see the example of appending the string by using position and length as parameters.

#include<iostream>
using namespace std;
int main()
{
 string str1 = "Mango is my favourite" ;
string str2 ="fruit";
cout<<"Before appending, string value is :" <<str1<<'\n';
str1.append(str2,0,5);
cout<<"After appending, string value is :" <<str1<<'\n';
return 0;
} 

Output:

Before appending, string value is Mango is my favourite
After appending, string value is Mango is my favourite fruit

Example 3

Let's see another example.

#include<iostream>
using namespace std;
int main()
{
 string str1 = "Kashmir is nature";
str1.append("of  beauty",9) ;
cout<<"String value is :"<<str1;
return 0;
} 

Output:

String value is Kashmir is nature of beauty

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