Java examples for java.lang:String Unicode
Checks if the String contains only unicode letters or digits. null will return false.
//package com.java2s; public class Main { public static void main(String[] argv) { String str = "java2s.com"; System.out.println(isAlphanumeric(str)); }/* www .j av a 2 s. co m*/ /** * <p>Checks if the String contains only unicode letters or digits.</p> * * <p><code>null</code> will return <code>false</code>. * An empty String ("") will return <code>true</code>.</p> * * <pre> * isAlphanumeric(null) = false * isAlphanumeric("") = true * isAlphanumeric(" ") = false * isAlphanumeric("abc") = true * isAlphanumeric("ab c") = false * isAlphanumeric("ab2c") = true * isAlphanumeric("ab-c") = false * </pre> * * @param str the String to check, may be null * @return <code>true</code> if only contains letters or digits, * and is non-null */ public static boolean isAlphanumeric(String str) { if (str == null) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if (Character.isLetterOrDigit(str.charAt(i)) == false) { return false; } } return true; } /** * Gets a String's length or <code>0</code> if the String is <code>null</code>. * * @param str * a String or <code>null</code> * @return String length or <code>0</code> if the String is <code>null</code>. * @since 2.4 */ public static int length(String str) { return str == null ? 0 : str.length(); } }