Extract numbers from an alpha numeric string using android

后端 未结 3 2010
粉色の甜心
粉色の甜心 2021-01-18 10:14

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

3条回答
  •  青春惊慌失措
    2021-01-18 11:05

    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

提交回复
热议问题