Java - Write code to get Digits Only String

Requirements

Write code to get Digits Only String

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String numString = "book2s.com";
        System.out.println(getDigitsOnlyString(numString));
    }//from   w  w  w.  j  a v  a  2  s.com

    public static String getDigitsOnlyString(String numString) {
        StringBuilder sb = new StringBuilder();
        for (char c : numString.toCharArray()) {
            if (Character.isDigit(c)) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Related Exercise