Extract numbers from alphanumeric string using android

I need to extract only numeric values ​​from String str="sdfvsdf68fsdfsf8999fsdf09" . How can I extract numbers from an alpha numeric string in android?

+6
source share
3 answers
 String str="sdfvsdf68fsdfsf8999fsdf09"; String numberOnly= str.replaceAll("[^0-9]", ""); 

update:

 String str="fgdfg12Β°59'50\" Nfr | gdfg: 80Β°15'25\" Efgd"; String[] spitStr= str.split("\\|"); String numberOne= spitStr[0].replaceAll("[^0-9]", ""); String numberSecond= spitStr[1].replaceAll("[^0-9]", ""); 
+35
source
 public static String getOnlyNumerics(String str) { if (str == null) { return null; } StringBuffer strBuff = new StringBuffer(); char c; for (int i = 0; i < str.length() ; i++) { c = str.charAt(i); if (Character.isDigit(c)) { strBuff.append(c); } } return strBuff.toString(); } 
+4
source
 public static int extractNumberFromAnyAlphaNumeric(String alphaNumeric) { alphaNumeric = alphaNumeric.length() > 0 ? alphaNumeric.replaceAll("\\D+", "") : ""; int num = alphaNumeric.length() > 0 ? Integer.parseInt(alphaNumeric) : 0; // or -1 return num; } 

You can set the value to 0 or -1 (what to do if there is no number at all in the alphanumeric format) according to your needs

+1
source

Source: https://habr.com/ru/post/916540/


All Articles