cpp str append code example

Example 1: c++ append a char to a string

// string::operator+=
#include <iostream>
#include <string>

int main ()
{
  std::string name ("John");
  std::string family ("Smith");
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character

  std::cout << name;
  return 0;
}

Example 2: append string to another string c++

// appending to string
#include <iostream>
#include <string>

int main ()
{
  // easy way
  std::string str = "Hello";
  std::string str2 = " World";
  std::cout << str + str2 << std::endl;
  return 0;
}

Tags:

Java Example