remove Non Digits - Java java.lang

Java examples for java.lang:char

Description

remove Non Digits

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String text = "java2s.com";
        System.out.println(removeNonDigits(text));
    }/*  ww w. j ava  2  s.c o m*/

    public static String removeNonDigits(String text) {
        if (text == null)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            if (Character.isDigit(text.charAt(i))) {
                sb.append(text.charAt(i));
            }
        }
        return sb.toString();
    }
}

Related Tutorials