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 @   yub4by  阅读(133)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示