天天看點

C++ append

append函數是向string的後面追加字元或字元串。頭檔案是<string>.

1).向string的後面加C-string

string s = “hello “; const char *c = “out here “;

s.append(c); // 把c類型字元串s連接配接到目前字元串結尾

s = “hello out here”;

2).向string的後面加C-string的一部分

string s=”hello “;const char *c = “out here “;

s.append(c,3); // 把c類型字元串s的前n個字元連接配接到目前字元串結尾

s = “hello out”;

3).向string的後面加string

string s1 = “hello “; string s2 = “wide “; string s3 = “world “;

s1.append(s2); s1 += s3; //把字元串s連接配接到目前字元串的結尾

s1 = “hello wide “; s1 = “hello wide world “;

4).向string的後面加string的一部分

string s1 = “hello “, s2 = “wide world “;

s1.append(s2, 5, 5); 把字元串s2中從5開始的5個字元連接配接到目前字元串的結尾

s1 = “hello world”;

string str1 = “hello “, str2 = “wide world “;

str1.append(str2.begin()+5, str2.end()); //把s2的疊代器begin()+5和end()之間的部分連接配接到目前字元串的結尾

str1 = “hello world”;

5).向string後面加多個字元

string s1 = “hello “;

s1.append(4,’!’); //在目前字元串結尾添加4個字元!

s1 = “hello !!!!”;

---------------------

作者:wxn704414736

來源:CSDN

原文:https://blog.csdn.net/wxn704414736/article/details/78551886

版權聲明:本文為部落客原創文章,轉載請附上博文連結!

c++

繼續閱讀