Common uses of the C + + string Append method

Source: Internet
Author: User

The

Append function is to append a character or string to the back of a string.
1). Add c-string
String s = "Hello" to the back of the string; const char *c = "Out Here";
S.append (c);//Concatenate C-type string s to the end of the current string
s = "Hello out here";
2). Add a portion of c-string to the back of the string
string s= "Hello"; const char *c = "Out Here";
S.append (c,3);//The first n characters of type C string s connect prompt to the end of the current string
s = "Hello out";
3). Add string
String s1 = "Hello" to the back of string; String s2 = "Wide"; string s3 = "World";
S1.append (S2); S1 + = s3;//Connect the string s to the end of the current string
S1 = "Hello wide"; S1 = "Hello Wide World";
4). Add a string to the back of the string
string s1 = "Hello", s2 = "Wide World";
S1.append (S2, 5, 5);////5 characters from 5 in the string S2 connect prompt to the end of the current string
S1 = "Hello World";
String str1 = "Hello", str2 = "Wide World";
Str1.append (Str2.begin () +5, Str2.end ()), and//connects the portion between the S2 iterator begin () +5 and End () to the end of the current string
str1 = "Hello World";
5). Add more characters to the string after
string s1 = "Hello";
S1.append (4, '! ');//Add 4 characters at the end of the current string!
S1 = "Hello!!!!";

C + + string append () Add text

Use append () to add text to common methods:

Add another complete string directly:

such as Str1.append (STR2);

Add a substring of another string:

such as Str1.append (STR2, 11, 7);

Add several of the same characters:

such as Str1.append (5, '. ');

Note that the number in the previous character is behind. The above code means to add 5 "." After str1.

Example:

     //========================================  #include<iostream>using namespacestd; //========================================            intMain () {stringstr1="I like C + +"; stringStr2=", I like the world."; stringstr3="Hello"; stringSTR4 ("Hi"); //====================================  str1.append (STR2); Str3.append (STR2, One,7); Str4.append (5,'.'); //====================================  cout<<str1<<Endl; cout<<str3<<Endl; cout<<str4<<Endl; System ("Pause"); return 0; }            //========================================  

Run the result as

I like C++,i.

Hello World.

Hi.....

Common uses of the C + + string Append method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.