ANDROID! How to get Numbers from a String?

 If you have a String Like ISHAN878 and you want to seperate/get the numbers from the String, here is the following code that will help you to get the same!

 
//pass you value into the method like

 String value = Ishan878Hi;
String number = getNumberFromString(value);


public static String getNumberFromString(String value){
char[] chars = value.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
if(Character.isDigit(c)){
sb.append(c);
}
}
return String.valueOf(sb);
}

// you will get the output: 878

:)





Comments