Extract numbers from an alpha numeric string using android
14,190
Solution 1
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]", "");
Solution 2
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();
}
Solution 3
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 no number is found in the alphanumeric at all) as per your needs
Related videos on Youtube
Comments
-
Prasanth_AndroidJD 4 months
I have to extract only numeric values from
String str="sdfvsdf68fsdfsf8999fsdf09"
. How can I extract numbers from an alpha numeric string in android?-
assylias over 10 years
-
-
Prasanth_AndroidJD over 10 yearsIf used this I am getting only alphabets, but I need only numeric values
-
Siddharth Lele over 10 years@Prasanth_AndroidJD: You might consider selecting this answer as the correct solution by marking it to go along with your thanks. ;-)
-
Prasanth_AndroidJD over 10 yearsstring str="fgdfg12°59'50" Nfr | gdfg: 80°15'25" Efgd" , can u plz help me here to extract this value to number only
-
Mohammed Azharuddin Shaikh over 10 years@Prasanth_AndroidJD Replace your string with this String str = "fgdfg12°59'50\" Nfr | gdfg: 80°15'25\" Efgd";.
-
Prasanth_AndroidJD over 10 yearsyes u r right, but ma problem is i had ma values in ma table as String str= "fgdfg12°59'50" Nfr | gdfg: 80°15'25" Efgd", so here how can i add \ to each(double quotes ") .. thank you in advance
-
Mohammed Azharuddin Shaikh over 10 yearsget value from Table by cursor, assign to string= cursor.getString(); String numberOnly= string.str.replaceAll("[^0-9]", "");
-
Prasanth_AndroidJD over 10 yearsHere in this String str= "fgdfg12°59'50" Nfr | gdfg: 80°15'25" Efgd",after (|) I have to separate the numeric values into 2 values like 125950 801525, Thanks in advance
-
Prasanth_AndroidJD over 10 yearsHow to extract for this :: <html>... <aherf ....idh=1002> HI..<ahref=....>HOW R U..</html>, Here I have extract Hi.. and How R U.. only as my Output, how can i extract for this scenario .. Help please regarding this issue.. Thanks in advance again..