Extract numbers from an alpha numeric string using android

后端 未结 3 1960
粉色の甜心
粉色の甜心 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:01

    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();
    }
    

提交回复
热议问题