Java examples for java.lang:String Trim
Cleans a String by reducing its contents to characters adhering to a reduced, single-case alphabet.
//package com.java2s; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.IOException; import java.util.List; public class Main { public static void main(String[] argv) { String string = "java2s.com"; System.out.println(cleanAlphabet(string)); }//from ww w . j ava2s . c om /** * The character used in listings of stories by letter where the letter * prefix is numeric. * * @see cleanAlphabet */ public final static char NON_ALPHABET_CHARACTER = '1'; /** * This is just a convenience constant to avoid instantiating a String for * comparisons and such */ public static final String EMPTY_STRING = ""; /** * Cleans a String by reducing its contents to characters adhering to a * reduced, single-case alphabet. Valid characters in the returned String * will be between 'A' to 'Z' and NON_ALPHABET_CHARACTER. Invalid characters * will be replaced by NON_ALPHABET_CHARACTER. * * @param s * The String to clean. * @return A String rebuild in a reduced alphabet. */ public static String cleanAlphabet(final String string) { String process = noNull(string).toUpperCase(); StringBuffer clean = new StringBuffer(process.length()); for (int i = 0; i < process.length(); i++) { char c = process.charAt(i); if (c < 'A' || c > 'Z') clean.append(NON_ALPHABET_CHARACTER); else clean.append(c); } return clean.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; } }