如何从C-字符串中删除第一个字符? [英] How to remove first character from C-string?

查看:191
本文介绍了如何从C-字符串中删除第一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮帮我吗?我需要从的char * 用C删除的第一个字符。

例如,的char *内容包含的'\\ n'字符作为数组中的第一个字符。我需要发现和消除这个人物,修改原始变量之后其被消毒。

谁能帮我用code?我完全新的C,和刚才似乎无法推测出来。


解决方案

 如果(内容[0] =='\\ n')
    的memmove(内容,内容+ 1,strlen的(内容));

或者,如果该指针可以修改

 如果(内容[0] =='\\ n')的内容++;

Can anyone please help me? I need to remove the first character from a char * in C.

For example, char * contents contains a '\n' character as the first character in the array. I need to detect and eliminate this character, modifying the original variable after its been "sanitized".

Can anyone help me with the code? I'm completely new to C, and just can't seem to figure it out.

解决方案

if (contents[0] == '\n') 
    memmove(contents, contents+1, strlen(contents));

Or, if the pointer can be modified:

if (contents[0] == '\n') contents++;

这篇关于如何从C-字符串中删除第一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆