Java examples for java.lang:char
Removes all non-alphanumeric characters from the given String.
//package com.java2s; public class Main { public static void main(String[] argv) { String string = "java2s.com"; System.out.println(cleanAlphaNumeric(string)); }/* ww w . jav a 2s . c o m*/ /** * This is just a convenience constant to avoid instantiating a String for * comparisons and such */ public static final String EMPTY_STRING = ""; /** * Removes all non-alphanumeric characters from the given String. */ public static String cleanAlphaNumeric(final String string) { String process = noNull(string); StringBuffer buffer = new StringBuffer(process.length()); for (int i = 0; i < process.length(); i++) { char c = process.charAt(i); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) buffer.append(c); } return buffer.toString(); } /** * Substitutes a null variable with an empty String. */ public static String noNull(final Object string) { if (string == null) return EMPTY_STRING; // Don't call toString twice. String ser = string.toString(); return ser == null ? EMPTY_STRING : ser; } }