可以使用字符串拼接函数 +,将两个字符串拼接起来,形成一个新的字符串。

例如:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str1 = 'Hello, ';  
    string str2 = 'world!';
    string str3 = str1 + str2;

    cout << str3 << endl;

    return 0;
}

输出:

Hello, world!

如果需要合并多个文本,可以使用循环进行字符串拼接,例如:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = '';
    string text[] = {'Hello', ', ', 'world', '!'};

    for(int i=0; i<4; i++) {
        str += text[i];
    }

    cout << str << endl;

    return 0;
}

输出:

Hello, world!
C++ 字符串合并:使用 + 运算符拼接文本

原文地址: https://www.cveoy.top/t/topic/nnqu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录