Java - Write code to remove Non Letter Or Digits

Requirements

Write code to remove Non Letter Or Digits

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        System.out.println(removeNonLetterOrDigits(string));
    }// w  w w .  j  av  a 2s.  co  m

    public static String removeNonLetterOrDigits(String string) {
        StringBuilder builder = new StringBuilder(string);
        for (int i = 0; i < builder.length(); i++) {
            char c = builder.charAt(i);
            if (!(Character.isLetterOrDigit(c) || Character.isWhitespace(c))
                    || c == '\'') {
                builder.deleteCharAt(i);
                i--;
            }
        }
        return builder.toString();
    }
}

Related Exercise