C++ 将一个字符数组的一部分复制到另一个字符数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13571032/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:29:58  来源:igfitidea点击:

C++ copy a part of a char array to another char array

c++

提问by James Diaz

I'm trying to copy only a portion of a string (or char *) into another string (or another char *)

我试图将字符串(或字符 *)的一部分复制到另一个字符串(或另一个字符 *)中

char * first_string = "Every morning I"
char * second_string = "go to the library, eat breakfast, swim."
char * final_string;

I would like to copy partof the second_string into the first_string.

我想将second_string 的一部分复制到 first_string 中。

For Example:

例如:

Every morning I eat breakfast.

每天早上我都吃早餐。

What is the function that allows you to copy only a portion of a string, starting from a specific point in the string?

允许您从字符串中的特定点开始仅复制字符串的一部分的函数是什么?

Note:I don't want to use string variables, but char *, or even char arrays, if possible.

注意:如果可能,我不想使用字符串变量,而是使用 char *,甚至是 char 数组。

回答by Luchian Grigore

It's std::copy, but with your code it would result in undefined behavior, because you have pointers to string literals, which are illegal to modify.

它是std::copy,但是对于您的代码,它会导致未定义的行为,因为您有指向字符串文字的指针,而这些指针是非法修改的。

You'll need something like

你需要类似的东西

char first_string[256] = "Every morning I";
char second_string[256] = "go to the library, eat breakfast, swim.";

std::copy(
    &second_string[23],
    &second_string[36],
    &first_string[strlen(first_string)]
);

Indices might be off.

指数可能会关闭。

回答by Axel

If you really want to, use strcat (if you don't use strings, you should be ok with the C-functions anyway):

如果您真的想要,请使用 strcat (如果您不使用字符串,则无论如何您都应该可以使用 C 函数):

const char * first_string = "Every morning I";
const char * second_string = "go to the library, eat breakfast, swim.";
char final_string[80]; // make sure it's big enough - I haven't counted ;-)

strcpy(final_string, first_string);     // copy to destination
strcat(final_string, second_string+18); // append part of the second string

回答by Innkeeper

You can use strstr()to search for the begining of the part you want, then strcat()to concatenate.

您可以使用strstr()搜索所需部分的开头,然后strcat()进行连接。

char * first_string = "Every morning I";
char * second_string = "go to the library, eat breakfast, swim.";
char * final_string;

char* s = "eat";
char* r = strstr(second_string, s);

strcat(final_string, first_string);
strcat(final_string, " ");
strcat(final_string, r);

回答by Guarita

I'd use 'strncat()' like this:

我会像这样使用“strncat()”:

const char * first_string = "Every morning I";
const char * second_string = "go to the library, eat breakfast, swim.";

char final_string [200];

//Copies first string first
strcpy(final_string, first_string);

//Copies second string
strncat(final_string, second_string[ text_position ], text_length);

Replace text_positionwith the position of second_stringfrom which you may want to start copying text and replace text_lengthwith the length of the portion of text you want to copy.

替换text_positionsecond_string您可能想要开始复制文本的位置,并替换text_length为您想要复制的文本部分的长度。

This way you may copy separate portions of text and not necessarily from a point of the string to the end.

通过这种方式,您可以复制文本的不同部分,而不必从字符串的某个点复制到末尾。