如何从字符串中删除字符 [英] how to remove a character from a string

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

问题描述

您好,

我的问题很容易解释。

我有以下字符串:


" table +相机


我要删除+号:

" tablecamera"。


如何我这样做了吗?

感谢您的帮助。

Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.

推荐答案



周三, 2004年4月7日,Toto写道:

On Wed, 7 Apr 2004, Toto wrote:

我有以下字符串:
" table + camera"
我要删除+号:
tablecamera。

我该怎么做?

I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".

How do i do that ?



#include< string.h>


char s [] =" table + camera";

char * p;


while((p = strchr(s,'') +''))!= NULL)

strcpy(p,p + 1);


请务必阅读有关字符串操作的常见问题解答。


HTH,

-Arthur


#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,''+'')) != NULL)
strcpy(p, p+1);

Make sure to read the FAQ on string manipulation, too.

HTH,
-Arthur


Toto< ma*@nospam.com>这样说:
Toto <ma*@nospam.com> spoke thus:
" table + camera"
我要删除+号:
" tablecamera"。
"table+camera"
and I want to remove the + sign:
"tablecamera".



好​​吧,我希望这不是一个功课问题,因为我会发一个

答案:


void rem_char(char * s,char t)

{

if(!s ||!* s)返回;


while(* s ++!= t)if(!* s)return;

while(*(s-1)= * s ++);

}


我不保证它符合要求,但它产生了正确的

结果对我而言。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Well, I hope this isn''t a homework question, because I''ll post an
answer:

void rem_char( char *s, char t )
{
if( !s || !*s ) return;

while( *s++ != t ) if( !*s ) return;
while( *(s-1)=*s++ );
}

I don''t guarantee that it''s conforming, but it produced correct
results for me.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


" Arthur J. O''Dwyer"写道:
"Arthur J. O''Dwyer" wrote:

2004年4月7日星期三,Toto写道:

On Wed, 7 Apr 2004, Toto wrote:

我有以下字符串:
" table +相机
我要删除+号:
tablecamera。

我该怎么做?

I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".

How do i do that ?



#include< string.h>

char s [] =" table + camera";
char * p;

while((p = strchr) (s,''+''))!= NULL)
strcpy(p,p + 1);



#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,''+'')) != NULL)
strcpy(p, p+1);




这只能保证工作如果'+''是字符串中的最后一个字符

。否则源和

目的地重叠,这对于strcpy()来说是禁止的。一个

安全替代品


memmove(p,p + 1,strlen(p + 1)+1);


....可以简化为


memmove(p,p + 1,strlen(p));


另外,Little Tin God可能会更好地用

替换while和


p = s;

while((p = strchr(p,''+''))!= NULL)


....因此不搜索,重新搜索和重新搜索

已知的部分's''没有''+''字符。


-
呃********* @ sun.com


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

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