在 C++ 中连接字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24255051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate char arrays in C++
提问by Matimont
I have the following code and would like to end up with a char such as: "Hello, how are you?" (this is just an example of what I'm trying to achieve)
我有以下代码,并希望得到一个字符,例如:“你好,你好吗?” (这只是我试图实现的一个例子)
How can I concatenate the 2 char arrays plus adding the "," in the middle and the "you?" at the end?
如何连接 2 个字符数组并在中间添加“,”和“你”?在末尾?
So far this concatenates the 2 arrays but not sure how to add the additional characters to my final char variable I want to come up with.
到目前为止,这连接了 2 个数组,但不确定如何将附加字符添加到我想提出的最终 char 变量中。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char foo[] = { "hello" };
char test[] = { "how are" };
strncat_s(foo, test, 12);
cout << foo;
return 0;
}
EDIT:
编辑:
This is what I came up with after all your replies. I'd like to know if this is the best approach?
这是我在你所有回复后想到的。我想知道这是否是最好的方法?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char foo[] = { "hola" };
char test[] = { "test" };
string foos, tests;
foos = string(foo);
tests = string(test);
string concat = foos + " " + tests;
cout << concat;
return 0;
}
回答by quantdev
In C++, use std::string
, and the operator+
, it is designed specifically to solve problems like this.
在 C++ 中,使用std::string
和operator+
,它是专门为解决此类问题而设计的。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string foo( "hello" );
string test( "how are" );
cout << foo + " , " + test;
return 0;
}
回答by Nayana Adassuriya
Best thing is use std::string
in C++ as other answers. If you really need to work with char try this way. didn't tested.
最好的办法是std::string
在 C++ 中用作其他答案。如果你真的需要使用 char 试试这种方式。没有测试。
const char* foo = "hello";
const char* test= "how are";
char* full_text;
full_text= malloc(strlen(foo)+strlen(test)+1);
strcpy(full_text, foo );
strcat(full_text, test);
回答by jav
Yes, in C++ use the +
operator for string concatenation.
But this will not work:
是的,在 C++ 中使用+
运算符进行字符串连接。但这行不通:
char[] + char[] + char[]
convert one array to std::string and it will:
将一个数组转换为 std::string ,它将:
std::string(char[]) + char[] + char[]
E.g.:
例如:
#include <iostream>
int main()
{
const char a[] = "how ";
const char b[] = "are ";
const char c[] = "you ";
std::cout << std::string( a + b + c ) << "\n"; // Error
std::cout << std::string(a) + b + c << "\n"; // Fine
}
回答by user8980253
If you dont want to use string you can do it simply by taking an other array and storing both arrays in it with loop
如果您不想使用字符串,您可以简单地通过获取另一个数组并使用循环将两个数组存储在其中来完成
#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int main(){
char fname[30],lname[30],full_name[60];
int i,j;
i=0;j=0; // i is index of fname and j is index for lname
cout<<"Enter your first name: ";
gets(fname);
cout<<"Enter your last name: ";
gets(lname);
for (i;fname[i]!='##代码##';i++){
full_name[i] = fname[i];
}
cout<<"i ="<<i;
full_name[i]=' ';
i = i + 1;
for (i,j;lname[j]!='##代码##';i++,j++){
full_name[i] = lname[j];
}
cout<<"Your full name is: "<<full_name<<endl;
system("pause");
return 0;
}