16.1.3【string字符串拼接】

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;


/*
    3.1.4 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个字符连接到字符串结尾
*/


void test1()
{
    string s1 = "you";
    s1 += " like play games";
    cout << s1 << endl;

    s1 += ':';
    cout << s1 << endl;

    string s2 = "CSGO LOL";
    s1 += s2;
    cout << s1 << endl;

    string s3 = "i";
    s3.append(" love gta");
    cout << s3 << endl;

    s3.append(" and dirt, etc.", 9);
    cout << s3 << endl;

    s3.append(" "+s2);
    cout << s3 << endl;

    s3.append(s2, 5, 3); //注意下标从0开始;5表示从下标5开始截取,3表示截取个数为3
    cout << s3 << endl;
}


int main()
{
    test1();

    system("pause");
    return 0;
}

posted @ 2021-04-22 22:05  yub4by  阅读(132)  评论(0编辑  收藏  举报