×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Appending text to the string 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

C++ STL - string::append() Function

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);

Parameter(s)

  • 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.

Example for appending text to the string in C++ STL

#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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.