C++ String Concatenating

Introduction

We can add a string literal to our string using the compound operator +=:

#include <iostream> 
#include <string> 

int main() /*from   w w  w  . j  a va 2 s. co  m*/
{ 
    std::string s = "Hello "; 
    s += "World."; 
    std::cout << s; 
} 

We can add a character to our string using the += operator:

#include <iostream> 
#include <string> 

int main() //w  w  w .  j a v a  2s  .com
{ 
    std::string s = "Hello"; 
    char c = '!'; 
    s += c; 
    std::cout << s; 
} 

We can add another string to our string using the + operator.

We say we concatenate the strings:

#include <iostream> 
#include <string> 

int main() //from   w  w w .  j  a v a  2s.  co m
{ 
    std::string s1 = "Hello "; 
    std::string s2 = "World."; 
    std::string s3 = s1 + s2; 
    std::cout << s3; 
} 

Type string is the so-called class-template.

It is implemented using templates.




PreviousNext

Related