Java - Write code to remove Non Digits from a string

Requirements

Write code to remove Non Digits from a string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String text = "book2s.com";
        System.out.println(removeNonDigits(text));
    }/*from  w w  w.j a  va2  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 Exercise