从字符串中删除所有数据。 [英] Strip everything out of a string expect numbers.

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

问题描述

我需要执行以下操作:我们的应用程序从

字符串构建信息,该字符串存储在缓冲区中,该缓冲区可以是以下任何一种

格式:ABCD0102, ABCDEF0102,AB * CD * 01 * 02,AB * CDEF * 01 * 02。所需的唯一

信息是最后的4位数。

asterix分隔的新格式是升级。我需要解决这个问题,而且还需要能够处理旧版本的格式。之前,它只会通过字符串中的位置来解析商店。和区域 (

我需要的信息)

..

..

..

memcpy(store,buff + strlen(buff)-4,2);

memcpy(area,buff + strlen(buff)-2,2);

..

..

..

但是,现在我在想我是否可以去除除了四个以外的一切

总是在那里的数字,在我到达memcopy'和'/ b $ b存储在新缓冲区之前,它仍然可以正常工作。我正在尝试

sscanf,但是不能让它起作用。


/ * strip(buff,new_buff)* /


memcpy(store,new_buff + strlen(new_buff)-4,2);

memcpy(area,new_buff + strlen(new_buff)-2,2);

I need to do the following: Our application builds information from a
string that is stored in a buffer that can be any of the following
formats: ABCD0102, ABCDEF0102, AB*CD*01*02, AB*CDEF*01*02. The only
information needed is the 4 digits at the end. The new format with
asterix seperation is an "upgrade" that I need to work around but also
be able to handle the older formats as well. Before, it would just go
by the position in the string to parse the "store" and "area" (the
information I need)
..
..
..
memcpy(store, buff+strlen(buff)-4,2);
memcpy(area, buff+strlen(buff)-2,2);
..
..
..
But, now I was thinking if I could just strip everything but the four
digits that are always there, before I do get to the memcopy''s and
store that in a new buffer, it will still work fine. I was trying
sscanf, but can''t get it to work.

/*strip(buff, new_buff)*/

memcpy(store, new_buff+strlen(new_buff)-4,2);
memcpy(area, new_buff+strlen(new_buff)-2,2);

推荐答案




jcf写道:


jcf wrote:
我需要做的以下:我们的应用程序从存储在缓冲区中的字符串构建信息,该缓冲区可以是以下任何格式:ABCD0102,ABCDEF0102,AB * CD * 01 * 02,AB * CDEF * 01 * 02。唯一需要的信息是最后的4位数字。具有
星号分隔的新格式是升级。我需要解决,但也需要


这是'星号'。 (除非你碰巧与一个令人尊敬的德鲁伊人签订了良好的

条款)和分离。

也能够处理旧的格式。之前,它只是通过字符串中的位置来解析存储按钮。和区域 (我需要的信息)



memcpy(存储,buff + strlen(buff)-4,2);
memcpy (区域,buff + strlen(buff)-2,2);



但是,现在我想我是否可以除了四个之外的所有东西<总是在那里的数字,在我进入memcopy'并且
存储在新缓冲区之前,它仍然可以正常工作。我正在尝试
sscanf,但是不能让它工作。

/ * strip(buff,new_buff)* /

memcpy(store,new_buff) + strlen(new_buff)-4,2);
memcpy(area,new_buff + strlen(new_buff)-2,2);
I need to do the following: Our application builds information from a
string that is stored in a buffer that can be any of the following
formats: ABCD0102, ABCDEF0102, AB*CD*01*02, AB*CDEF*01*02. The only
information needed is the 4 digits at the end. The new format with
asterix seperation is an "upgrade" that I need to work around but also
That''s "asterisk" (unless you happen to be on good
terms with a venerable Druid) and "separation."
be able to handle the older formats as well. Before, it would just go
by the position in the string to parse the "store" and "area" (the
information I need)
.
.
.
memcpy(store, buff+strlen(buff)-4,2);
memcpy(area, buff+strlen(buff)-2,2);
.
.
.
But, now I was thinking if I could just strip everything but the four
digits that are always there, before I do get to the memcopy''s and
store that in a new buffer, it will still work fine. I was trying
sscanf, but can''t get it to work.

/*strip(buff, new_buff)*/

memcpy(store, new_buff+strlen(new_buff)-4,2);
memcpy(area, new_buff+strlen(new_buff)-2,2);




isdigit( )函数来自< ctype.h>可以帮助你

决定每个字符是数字还是非数字。

来自< string.h>的strcspn()函数可以给你一个

简单的方法来跳过所有领先的非数字。如果

你需要更彻底的验证(例如,所有领先的

非数字必须是大写字母或必须有

至少四个但不超过六个领先的非数字

或其他类似的东西,你需要描述它。


-
Er*********@sun.com





Eric Sosman写道:


Eric Sosman wrote:
jcf写道:
我需要执行以下操作:我们的应用程序从存储在缓冲区中的字符串构建信息,该缓冲区可以是以下任何格式:ABCD0102,ABCDEF0102,AB * CD * 01 * 02,AB * CDEF * 01 * 02。唯一需要的信息是最后的4位数字。具有
星号分隔的新格式是升级。我需要解决,但
I need to do the following: Our application builds information from a
string that is stored in a buffer that can be any of the following
formats: ABCD0102, ABCDEF0102, AB*CD*01*02, AB*CDEF*01*02. The only
information needed is the 4 digits at the end. The new format with
asterix seperation is an "upgrade" that I need to work around but also



这是'星号'。 (除非你碰巧与古老的德鲁伊有着良好的关系)和分离。



That''s "asterisk" (unless you happen to be on good
terms with a venerable Druid) and "separation."

也能够处理旧的格式。之前,它只是通过字符串中的位置来解析存储按钮。和区域 (我需要的信息)



memcpy(存储,buff + strlen(buff)-4,2);
memcpy (区域,buff + strlen(buff)-2,2);



但是,现在我想我是否可以除了四个之外的所有东西<总是在那里的数字,在我进入memcopy'并且
存储在新缓冲区之前,它仍然可以正常工作。我正在尝试
sscanf,但是不能让它工作。

/ * strip(buff,new_buff)* /

memcpy(store,new_buff) + strlen(new_buff)-4,2);
memcpy(area,new_buff + strlen(new_buff)-2,2);
be able to handle the older formats as well. Before, it would just go
by the position in the string to parse the "store" and "area" (the
information I need)
.
.
.
memcpy(store, buff+strlen(buff)-4,2);
memcpy(area, buff+strlen(buff)-2,2);
.
.
.
But, now I was thinking if I could just strip everything but the four
digits that are always there, before I do get to the memcopy''s and
store that in a new buffer, it will still work fine. I was trying
sscanf, but can''t get it to work.

/*strip(buff, new_buff)*/

memcpy(store, new_buff+strlen(new_buff)-4,2);
memcpy(area, new_buff+strlen(new_buff)-2,2);



来自<的isdigit()函数;文件ctype.h>可以帮助您确定每个字符是数字还是非数字。
来自< string.h>的strcspn()函数可以给你一个简单的方法来跳过所有领先的非数字。如果您需要更彻底的验证(例如,所有领先的
非数字必须是大写字母或必须至少有四个但不超过六个你需要对它进行描述。

-
Er ********* @ sun.com




这是sscanf对于新格式(AB * CD * 01 * 02)几乎有效的功能





sscanf(old,"%[ AZ,*]%2 [0-9] *%2 [0-9]",垃圾,商店,区域);


旧格式(ABCD0102):


sscanf(旧的,%[AZ,*]%2 [0-9]%2 [0-9]",垃圾,商店,区域);


所以,我想,我的问题是,他们是一种整合那些两个字符串的两个sscanf表达式的方法。比如

''丢弃大写字母和星号,得到两个数字

为store,扔掉一个星号,可能有也可能没有,得到

两个数字代表area''



Here is the sscanf function that almost works

for the new format (AB*CD*01*02):

sscanf(old, "%[A-Z,*]%2[0-9]*%2[0-9]",junk, store, area);

for the old format (ABCD0102):

sscanf(old, "%[A-Z,*]%2[0-9]%2[0-9]",junk, store, area);

So, I guess, really my question is, is their a way to consolidate those
two sscanf expressions that will work on both strings. Something like
''throw away leading uppercase letters and asterisks, get two numbers
for "store", throw away an asterisk, which may or may not be there, get
two numbers for "area"''


jcf写道:
我需要执行以下操作:我们的应用程序从存储在缓冲区中的字符串构建信息,该缓冲区可以是以下任何格式:ABCD0102,ABCDEF0102,AB * CD * 01 * 02,AB * CDEF * 01 * 02。唯一需要的信息是最后的4位数字。具有
星号分隔的新格式是升级。我需要解决,但也能够处理旧的格式。


[snipped]

但是,现在我在想我是否可以除了四个总是在那里的数字之外的一切,在我之前得到memcopy'和
存储在新的缓冲区,它仍然可以正常工作。我正在尝试
sscanf,但是不能让它工作。

/ * strip(buff,new_buff)* /

memcpy(store,new_buff) + strlen(new_buff)-4,2);
memcpy(area,new_buff + strlen(new_buff)-2,2);
I need to do the following: Our application builds information from a
string that is stored in a buffer that can be any of the following
formats: ABCD0102, ABCDEF0102, AB*CD*01*02, AB*CDEF*01*02. The only
information needed is the 4 digits at the end. The new format with
asterix seperation is an "upgrade" that I need to work around but also
be able to handle the older formats as well.
[snipped]
But, now I was thinking if I could just strip everything but the four
digits that are always there, before I do get to the memcopy''s and
store that in a new buffer, it will still work fine. I was trying
sscanf, but can''t get it to work.

/*strip(buff, new_buff)*/

memcpy(store, new_buff+strlen(new_buff)-4,2);
memcpy(area, new_buff+strlen(new_buff)-2,2);




这个怎么样:


sscanf(szIn,"%[^ 0-9]%d%[^ 0-9]%d",szTemp,& i1,szTemp,& i2 );

if(i1> = 100)/ *这表示i1捕获了两个数字* /

{

i2 = i1%100 ;

i1 / = 100;

}


printf("%d - %d \ n",i1 ,i2);


您还可以使用sscanf的返回值(返回成功检索的项目金额
)。对于

szTemp变量可能有一个更好的解决方案,但是我不熟悉(s)scanf以了解是否有一个替代品。

。 />

但这只适用于一组有限的输入。


祝你好运,


-

Martijn
http://www.sereneconcepts。 nl


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

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