Remove none digital character from the decimal string that maybe contains none digit character. - Android java.lang

Android examples for java.lang:String Parse

Description

Remove none digital character from the decimal string that maybe contains none digit character.

Demo Code


//package com.java2s;
import android.text.TextUtils;

public class Main {
    /**//w  ww.ja v a  2 s  . c o m
     * Remove none digital character from the decimal string that maybe contains
     * none digit character.
     *
     * @param decimal
     *            the decimal string.
     * @return the new digital string only contains digital character.
     */
    public static String getPlain(String decimal) {
        if (TextUtils.isEmpty(decimal)) {
            decimal = "0";
        }
        char[] ch = new char[decimal.length()];
        int index = 0;
        for (int i = 0; i < decimal.length(); i++) {
            if (Character.isDigit(decimal.charAt(i))) {
                ch[index++] = decimal.charAt(i);
            }
        }
        return String.copyValueOf(ch, 0, index);
    }
}

Related Tutorials