A detailed explanation of common usage of C++ string append method

  • 2020-06-07 04:51:39
  • OfStack

C++ string append() add text

Common ways to add text using append() :

Add another complete string directly:

Such as str1. append (str2);

Add one substring of another string:

Such as str1. append (str2, 11, 7);

Add a few of the same characters:

Such as str1. append (5, '. ');

Note that the number comes before the character. The code above means adding five "." after str1.


//========================================
 
#include<iostream>
 
using namespace std;
 
//========================================
 
int main()
 
{
 
 string str1="I like C++";
 
 string str2=",I like the world.";
 
 string str3="Hello";
 
 string str4("Hi");
 
 //====================================
 
 str1.append(str2);
 
 str3.append(str2, 11, 7);
 
 str4.append(5, '.');
 
 //====================================
 
 cout<<str1<<endl;
 
 cout<<str3<<endl;
 
 cout<<str4<<endl;
 
 system("pause");
 
 return 0; 
 
}
 
//========================================

The operation result is


I like C++,I like the world.
Hello World.
Hi.....

Related articles: