如何查找字符串包含数值 [英] How to find string contains a numeric value

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

问题描述

他们在C ++中可用的任何功能可以确定字符串

包含数值。

值cabn为十六进制,整数,浮点数。即1256,即1256。 ,123.566, ,

" 0xffff"

Thnx

解决方案

4月4日,下午4:01,nishit.gu ... @ st.com写道:


他们在C ++中可用的任何功能都可以确定一个

string包含一个数值。

值cabn为十六进制,整数,浮点数。即1256,即1256。 ,123.566, ,

" 0xffff"



尝试strtol()。如果这给出了错误,那么尝试strtod()。如果这也是

给出错误,那么该字符串不包含数值。


blockquote>

4月3,9:01 pm,nishit.gu ... @ st.com写道:


他们在C ++中可用的任何功能都可以确定字符串

包含一个数值。

值cabn为十六进制,整数,浮点数。即1256,即1256。 ,123.566, ,

" 0xffff"



如果你想要一个C ++解决方案,那么发布新闻:comp.lang.c ++


你问题的一个答案是为数字写一个正式的语法

,然后用语法写一个解析器。


你可以用C函数手工完成它:

isdigit()

isxdigit()

ispunct()

isalpha()

和一点逻辑。


PS

它比看起来更难以实现。


只是因为某个数字并不意味着你可以在

中读到它。


例如:

1.0e99999


Thanx fr回复,但错误意味着......

strtol& strtod在失败时返回0并且我的字符串可以包含零

也就是0

那么我将如何区分???

和在strtol onre需要给出基值,我不确定

它可以是十六进制(16),十进制(10)???

Old Wolf写道:


4月4日下午4:01,nishit.gu ... @ st.com写道:


他们在C ++中可用的任何功能可以确定

字符串包含数值。

值cabn为十六进制,整数,浮点数。即1256,即1256。 ,123.566, ,

" 0xffff"



尝试strtol()。如果这给出了错误,那么尝试strtod()。如果这也是

给出错误,那么该字符串不包含数值。


Is their any fuction available in C++ that can determine that a string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff"
Thnx

解决方案

On Apr 4, 4:01 pm, nishit.gu...@st.com wrote:

Is their any fuction available in C++ that can determine that a
string contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff"

Try strtol(). If that gives an error then try strtod(). If that also
gives an error, then the string does not contain a numeric value.



On Apr 3, 9:01 pm, nishit.gu...@st.com wrote:

Is their any fuction available in C++ that can determine that a string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff"

If you want a C++ solution, then post in news:comp.lang.c++

One answer to your question is to write a formal grammar for numbers
and then use the grammar to write a parser.

You could do it by hand in C by using fuctions:
isdigit()
isxdigit()
ispunct()
isalpha()
and a bit of logic.

P.S.
It''s harder than it looks to get it right.

Just because something is a number doesn''t mean you can read it in
either.

For example:
1.0e99999


Thanx fr reply but what does an error means......
strtol & strtod returns 0 on failure and my string can contains zero
too i.e. "0"
So how will i differentiate???
and in strtol onre needs to give the base value, which i am not sure
it can be hex(16), decimal(10)???
Old Wolf wrote:

On Apr 4, 4:01 pm, nishit.gu...@st.com wrote:

Is their any fuction available in C++ that can determine that a
string contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff"


Try strtol(). If that gives an error then try strtod(). If that also
gives an error, then the string does not contain a numeric value.


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

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