如何在已有的字符串之前加另一段字符串(C++)

来源:百度知道 编辑:UC知道 时间:2024/05/27 16:28:49
如何在已有的字符串之前加另一段字符串(C++)

#include <iostream>
#include <string>

using namespace std;

int main()
{
//1.用string类
string s1("World");
string s2("Hello ");
string s3 = s2 + s1;
cout<<s3<<endl;

//2.函数
char *ch1 = "World";
char * ch2 = "Hello ";
char ch3[30]; //空间要够,否则就自己分配,new和strlen一起用

strcpy(ch3,ch2); //新版的VC中提供了更安全的版本:strcpy_s和strcat_s
strcat(ch3,ch1);
cout<<ch3<<endl;

}

string str1="字符串一";
string str2="字符串两加到字符串一";
str1=str2+str1;

string s1 = "sdgsgsdgsdgsdg";
string s2 = "geyweryahsarghs";

s1 = s2+s1;

不就ok了?