题
我需要连接两个常量字符像这样:
const char *one = "Hello ";
const char *two = "World";
我怎么会去这样做?
我通过从第三方库这些char*
s与C接口,所以我不能简单地使用std::string
代替。
解决方案
在您的示例的一个和 2 是字符指针,指向字符常数。您不能更改这些指针指向的字符常量。因此,类似的东西:
strcat(one,two); // append string two to string one.
将不起作用。相反,你应该有一个独立的变量(字符数组)来保存结果。是这样的:
char result[100]; // array to hold the result.
strcpy(result,one); // copy string one into the result.
strcat(result,two); // append string two to the result.
其他提示
的C的方法:
char buf[100];
strcpy(buf, one);
strcat(buf, two);
在C ++方法:
std::string buf(one);
buf.append(two);
在编译时的方法:
#define one "hello "
#define two "world"
#define concat(first, second) first second
const char* buf = concat(one, two);
如果您使用的是C ++,你为什么不使用std::string
而不是C风格的字符串?
std::string one="Hello";
std::string two="World";
std::string three= one+two;
如果您需要将此字符串传递给一个C-功能,简单地传递three.c_str()
使用std::string
:
#include <string>
std::string result = std::string(one) + std::string(two);
<强>更新强>改变
string total = string(one) + string(two);
到string total( string(one) + two );
出于性能原因(避免了串两个临时串总的结构)
const char *one = "Hello ";
const char *two = "World";
string total( string(one) + two ); // OR: string total = string(one) + string(two);
// string total(move(move(string(one)) + two)); // even faster?
// to use the concatenation in const char* use
total.c_str()
再举一个例子:
// calculate the required buffer size (also accounting for the null terminator):
int bufferSize = strlen(one) + strlen(two) + 1;
// allocate enough memory for the concatenated string:
char* concatString = new char[ bufferSize ];
// copy strings one and two over to the new buffer:
strcpy( concatString, one );
strcat( concatString, two );
...
// delete buffer:
delete[] concatString;
但除非你特别不希望或不能使用C ++标准库,使用std::string
可能是更安全的。
这与好像你正在使用C ++与C库,因此你需要用const char *
工作。
我建议包装那些const char *
到std::string
:
const char *a = "hello ";
const char *b = "world";
std::string c = a;
std::string d = b;
cout << c + d;
首先,你需要创建一些动态内存空间。然后,你可以strcat的两个字符串进去。或者你也可以使用C ++“串”类。旧-学校C的方法:
char* catString = malloc(strlen(one)+strlen(two)+1);
strcpy(catString, one);
strcat(catString, two);
// use the string then delete it when you're done.
free(catString);
新的C ++方法
std::string three(one);
three += two;
如果你不知道字符串的大小,你可以做这样的事情:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
const char* q1 = "First String";
const char* q2 = " Second String";
char * qq = (char*) malloc((strlen(q1)+ strlen(q2))*sizeof(char));
strcpy(qq,q1);
strcat(qq,q2);
printf("%s\n",qq);
return 0;
}
可以使用strstream
。它的正式弃用,但如果你需要使用C字符串工作它仍然是一个伟大的工具,我想。
char result[100]; // max size 100
std::ostrstream s(result, sizeof result - 1);
s << one << two << std::ends;
result[99] = '\0';
此将写one
然后two
到流,并且使用\0
附加端接std::ends
。如果两个字符串可能最终究竟写99
人物 - 所以没有空间将左书写\0
- 我们手动编写一个在最后的位置。
const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);
连接两个恒定字符指针没有在存储器的动态分配的使用的strcpy命令:
const char* one = "Hello ";
const char* two = "World!";
char* three = new char[strlen(one) + strlen(two) + 1] {'\0'};
strcat_s(three, strlen(one) + 1, one);
strcat_s(three, strlen(one) + strlen(two) + 1, two);
cout << three << endl;
delete[] three;
three = nullptr;