Android – how to extract numbers from strings

See the English answer > extract digits from a string in Java 13. I am writing an application to reset the password. You will receive a text message containing the reset code to confirm that you are the owner of the account. But I am trying to build the application, So that it can receive the received broadcast SMS and only get the specified integer from the SMS body. For the sample of SMS, we notice that you want to reset your password. The password reset code is 3242. Thank you

I want to get the reset code from the whole SMS Organization = 3242.. thank you

resolvent:

Using regular expressions

  String number = body.replaceAll("\\D+","");

Or use regular expressions such as [^ 0-9] to delete all non numbers

String number  = body.replaceAll("[^0-9]", "");

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>