How to use append in C++

How to use append in C++

Introduction

In C ++ append function for the string type of string appended behind the characters or character string

Common usage

1). Add C-string to the back of string

string s = "hello"; const char *c = "out here";
s.append( c ); // connect the c type string s to the end of the current string
s = "hello out here";

2). Add a part of C-string to the back of string

string s=”hello”; const char *c = “out here”;
s.append(c,3); // Connect the first n characters of the c type string s to the end of the current string
s = “hello out ";

3). Add string to the back of string

string s1 = "hello"; 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 part of string to the back of string

string s1 = "hello", s2 = "wide world";
s1.append(s2, 5, 5); Connect the 5 characters starting from 5 in the string s2 to the end of the current string
s1 = "hello world" ;
string str1 = "hello", str2 = "wide world";
str1.append(str2.begin()+5, str2.end()); //put the iterator of s2 begin()+5 and end() The part between is connected to the end of the current string
str1 = "hello world";

5). Add multiple characters to the string

string s1 = "hello";
s1.append(4,'!'); //Add 4 characters to the end of the current string!
s1 = "hello !!!";

Guess you like

Origin blog.csdn.net/m0_45388819/article/details/111407586