Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a number from a string [duplicate]

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

i like to get just the reset code=3242 from the whole sms body.. Thanks.

like image 555
heeleeaz Avatar asked Jul 29 '14 12:07

heeleeaz


People also ask

How do you extract a number from a string in Excel?

Extract Numbers from String in Excel (using VBA) Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetNumeric(A2). This will instantly give you only the numeric part of the string. Note that since the workbook now has VBA code in it, you need to save it with .

How do I extract numbers from a string in Python?

To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.

How can I extract a number from a string in Java?

Java code to extract digits or numbers from a string with logic and explanation. We'll use 2 methods: (1) using regex expression with replaceAll method of String object, and (2) using loop and ascii values of characters.


1 Answers

Use regex as

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

Or use a regex such as [^0-9] to remove all non-digits as

String number  = body.replaceAll("[^0-9]", "");
like image 142
Giru Bhai Avatar answered Oct 22 '22 16:10

Giru Bhai