从字符串中提取整数 [英] integer extraction from string

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

问题描述

我有一个输入参数字符串 const char * s
我知道它启动的字符序列是整数表示,这个序列可以是任何字符串长度,包括0.
在整数序列之后是字符序列,它不是整数表示。
例如23meeting,1h
是否有一些(内置)C函数可以提取整数?
例如,对于23meeting,可以执行此类操作:

I have an input argument string const char* s I know that it starts the sequence of chars which are integers representation ,this sequence could be of any length ,including 0. After the integer sequence follows sequence of chars which are not integer representation. for example "23meeting","1h". Is there some (builtin) C function which can extract the integer? For example for "23meeting" such operation could be performed :

int x = needed_function("23meeting"); //results x = 23

谢谢

推荐答案

atoi()应该可以正常工作。它应该停在第一个非数字字符。

atoi() should work fine for this. It should stop at the first non-numeric character.

int x = atoi("23meeting")

编辑:评论意味着atoi()不是线程安全的,或者是从语言中弃用的。这里有一些关于此函数的线程安全性的讨论:

A comment implies that atoi() is not thread safe or is deprecated from the language. There is some discussion of the thread safety of this function here:

为什么OSX将atoi / atof记录为不是线程安全的?

有人可以提供对atoi不是线程安全的引用?

Can someone provide a reference to atoi not being thread safe?

据我所知,atoi()在C99中是最新标准(7.20.1.2)。

And as far as I can tell atoi() is in C99 which is the latest standard (7.20.1.2).

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

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