1. 程式人生 > 其它 >c++ string字串拼接

c++ string字串拼接

主要用於在已有字串之後追加

函式原型:
string& operator+=(const char* str); //過載+=操作符
string& operator+=(const char c); //過載+=操作符
string& operator+=(const string& str); //過載+=操作符
string& append(const char *s); //把字串s連線到當前字串結尾
string& append(const char *s, int n); //把字串s的前n個字元連線到當前字串結尾
string& append(const string &s); //operator+=(const string& str)
string& append(const string &s, int pos, int n); //字串s中從pos開始的n個字元連線到字串結尾

  string s1 = "hello";   cout << s1 <<endl;   s1 += " world";   cout << s1 <<endl;   s1 += 's';   cout << s1 <<endl;   string s2 = " wanvei";   s1 += s2;   cout << s1 <<endl;   s1.append(" is");   cout << s1 <<endl;   s1.append("kkkkkk",4);
  cout << s1 <<endl;   s1.append(s2);   cout << s1 <<endl;   s1.append(s2,4,3);   cout << s1 <<endl; hello
hello world
hello worlds
hello worlds wanvei
hello worlds wanvei is
hello worlds wanvei iskkkk
hello worlds wanvei iskkkk wanvei
hello worlds wanvei iskkkk wanveivei