Appending text to the string using string::append() function in C++ STL

C++ STL: string::append() function with example: In this tutorial, we will learn to append text to the string using string::append() function.
Submitted by IncludeHelp, on July 02, 2018

append() is a library function of <string> header, it is used to append the extra characters/text in the string.

Syntax:

string& append(const string& substr);

Here,

  • string& is the reference to the string in which we are adding the extra characters.
  • substr is the extra set of character/sub-string to be appended.

There are some other variations (function overloading) of the function that are going to be used in the program.

Program:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string str = "Hello";
	string str2 = " ## ";
	string str3 = "www.google.com";

	cout<<str<<endl;

	//append text at the end 
	str.append (" world");
	cout<<str<<endl;

	//append 'str2'
	str.append (str2) ;
	cout<<str<<endl;

	//append space 
	str.append (" ");
	//append 'google' from str3
	str.append (str3.begin () +4, str3.begin () +10) ;

	cout<<str<<endl;

	return 0;
}

Output

    Hello
    Hello world
    Hello world ## 
    Hello world ##  google

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.