C语言 c 删除数组的第一个字符

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

c remove the first character of an array

c

提问by hkvega

I have a string:

我有一个字符串:

str1 = "abcabcabc";

How can I remove the first character? I would like the end result to be:

如何删除第一个字符?我希望最终结果是:

str1 = "bcabcabc";

回答by paxdiablo

If you have a character pointer to a string like:

如果您有一个指向字符串的字符指针,例如:

char *s = "This is my string";

then you can just do s++.

那你就可以了s++

If you have a character array,your best bet may be to have a pointer to that array as well:

如果您有一个字符数组,最好的办法可能是也有一个指向该数组的指针:

char s[] = "This is my string";
char *ps = s;

then you can do ps++and make sure you use psrather than s.

那么你可以做ps++并确保你使用ps而不是s.

If you don't want to have a separate pointer to your array then you can use memmoveto copy the data:

如果你不想有一个单独的指针指向你的数组,那么你可以memmove用来复制数据:

memmove (s, s+1, strlen (s+1) + 1); // or just strlen (s)

though none of those will work for an initially empty string so you'll have to check that first. Also keep in mind it's not advisable to attempt modifying string literalsin this way (or anyway, really) since it's undefined as to whether that's allowed.

尽管这些都不适用于最初为空的字符串,因此您必须先进行检查。还要记住,不建议尝试以这种方式(或任何方式,真的)修改字符串文字,因为它是否允许这样做是不确定的。

Another solution is to simply code up a loop:

另一种解决方案是简单地编写一个循环:

for (char *ps = s; *ps != '
char* s = "abcd";
char* substr = s + 1;
// substr == "bcd"
'; ps++) *ps = *(ps+1); *ps = '
char s[] = "abcd";
char* substr = s + 1;
// substr == "bcd"
';

This will work for all strings, empty or otherwise.

这将适用于所有字符串,无论是空字符串还是其他字符串。

回答by Mateen Ulhaq

Pointer tricks (zero-cost):

指针技巧(零成本):

char s[] = "abcd";
char* substr = s + 1;
memmove(s, substr, strlen(substr) + 1);
// s == "bcd"

Or:

或者:

char* src = "abcd";
char* substr = src + 1;
char dest[strlen(substr) + 1];
strcpy(dest, substr);
// dest == "bcd"


In-place via memmove:

就地通过memmove

std::string src = "abcd";
std::string dest = src.substr(1);
// dest == "bcd"

Notice that we must use char[]rather than char*, which would refer to read-only memory, as described here. Furthermore, one should not use strcpyin-place because the src and dest must not overlap for strcpy.

请注意,我们必须使用char[],而不是char*,这是指只读存储器,如所描述这里。此外,不应strcpy就地使用,因为 src 和 dest 不能重叠strcpy



Into a new string via strcpy:

通过strcpy以下方式转换为新字符串:

std::string src = "abcd";
std::string dest;
std::copy(src.begin() + 1, src.end(), std::back_inserter(dest));
// dest == "bcd"


Into a new string via C++'s std::string::substr:

通过 C++ 的新字符串std::string::substr

char str1 [] = "abcabcabc";


Into a new string via C++'s std::copy:

通过 C++ 的新字符串std::copy

str1 = &str1[1];


There's a couple dozen other ways (particularly when including C++), but I'll stop here. :)

还有几十种其他方式(尤其是在包含 C++ 时),但我会在这里停止。:)

回答by Adam

If you really meant to say

如果你真的想说

str1 = str1.substr(1);

Then the easiest thing is

那么最简单的就是

int index = 0; //index to cull
memmove( &word[ index ] , &word[ index +1], strlen( word ) - index) ;

If you want to modify the actual data, then you have to just move everything up one position. You can use a loop for that or memmove(). A recursive function is overkill.

如果您想修改实际数据,那么您只需将所有内容向上移动一个位置。您可以为此使用循环或memmove(). 递归函数是矫枉过正的。

If you really meant C++ and you're using the string object then you can use

如果您的意思是 C++ 并且您使用的是字符串对象,那么您可以使用

#include <stdio.h>
#include <conio.h>
main(){
   char a[10];
   int i;
   gets(a);

   for (i = 0; a[i] != '##代码##'; i++) {
      a[i] = a[i + 1];
   }

   printf("\n");
   puts(a);
   getch();
}

回答by Shreesh

Here is one way to do it:

这是一种方法:

##代码##

回答by Tincu Gabi

Well as far as i know if you are worried about memory allocation you have to copy (str1+1) into a new string that you personally allocate memory for, then free the first pointer. The really simple way to do it would be to just increment str1 with str1++; That would make it point one character farther than it used to and give you the desired result with just a line of code.

据我所知,如果您担心内存分配,您必须将 (str1+1) 复制到您个人为其分配内存的新字符串中,然后释放第一个指针。真正简单的方法是使用 str1++ 增加 str1;这将使它比以前指向一个字符更远,只需一行代码即可获得所需的结果。

回答by Monideep Bora

##代码##