如何从字符串中提取数字 [英] How to extract a number from a string
问题描述
我正在编写一个重置密码的应用程序,您将收到包含重置代码的短信,以确认您是该帐户的所有者。但我正在尝试以接收广播的方式构建应用程序接收到的短信并仅从短信主体获取指定的整数。
sms示例
我们注意到您要重置密码。密码重置代码是3242.,谢谢
I am writting an application whereby to reset a password you will be sent an sms containing a reset code to confirm you as the owner of the account.. But am trying to build the app in such a way that it receives the broadcast of the received sms and get the specified integer only from the sms body. Sample of the sms We noticed you want to reset your password. Password reset code is 3242., Thanks
我想从整个短信机构获得重置代码= 3242 ..
谢谢。
i like to get just the reset code=3242 from the whole sms body.. Thanks.
推荐答案
使用正则表达式
String number = body.replaceAll("\\D+","");
或者使用正则表达式,例如 [^ 0-9]
删除所有非数字
Or use a regex such as [^0-9]
to remove all non-digits as
String number = body.replaceAll("[^0-9]", "");
这篇关于如何从字符串中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!