Example usage for java.lang Character isLetterOrDigit

List of usage examples for java.lang Character isLetterOrDigit

Introduction

In this page you can find the example usage for java.lang Character isLetterOrDigit.

Prototype

public static boolean isLetterOrDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a letter or digit.

Usage

From source file:Main.java

/**
 * Anything other than letter and numbers are considered delimiters.  Remove start and end
 * delimiters since they are not relevant to search.
 *
 * @param query The query string to clean.
 * @return The cleaned query. Empty string if all characters are cleaned out.
 *//*ww w  . j  a  v a 2s  . c o  m*/
public static String cleanStartAndEndOfSearchQuery(String query) {
    int start = 0;
    while (start < query.length()) {
        int codePoint = query.codePointAt(start);
        if (Character.isLetterOrDigit(codePoint)) {
            break;
        }
        start += Character.charCount(codePoint);
    }

    if (start == query.length()) {
        // All characters are delimiters.
        return "";
    }

    int end = query.length() - 1;
    while (end > -1) {
        if (Character.isLowSurrogate(query.charAt(end))) {
            // Assume valid i18n string.  There should be a matching high surrogate before it.
            end--;
        }
        int codePoint = query.codePointAt(end);
        if (Character.isLetterOrDigit(codePoint)) {
            break;
        }
        end--;
    }

    // end is a letter or digit.
    return query.substring(start, end + 1);
}

From source file:Main.java

/**
 * Determines if the specified string contains only Unicode letters or
 * digits as defined by {@link Character#isLetterOrDigit(char)}
 * @param test The string to test//from  w  w  w. j  ava2 s . c om
 * @return true if the string is non-null, non-empty and contains only
 * characters that are unicode letters or digits
 * @see Character#isLetterOrDigit(char)
 */
public static boolean isLetterOrDigitOrUnderline(String test) {
    if (test == null || test.length() == 0) {
        return false;
    }

    for (int i = 0; i < test.length(); i++) {
        if (!Character.isLetterOrDigit(test.charAt(i)) && test.charAt(i) != '_') {
            return false;
        }
    }

    return true;
}

From source file:Main.java

/**
 * <p>/*  w  ww  . j  a va  2 s  .c  o m*/
 * Checks if the String contains only unicode letters or digits.
 * </p>
 * <p/>
 * <p>
 * <code>null</code> will return <code>false</code>. An empty String will return <code>true</code>.
 * </p>
 *
 * @param str the String to check
 * @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;
}

From source file:Main.java

/**
 * Finds the index of the first word that starts with the given prefix.
 * <p>/*w w w.ja va 2s . c  om*/
 * If not found, returns -1.
 *
 * @param text the text in which to search for the prefix
 * @param prefix the text to find, in upper case letters
 */
public static int indexOfWordPrefix(CharSequence text, String prefix) {
    if (prefix == null || text == null) {
        return -1;
    }

    int textLength = text.length();
    int prefixLength = prefix.length();

    if (prefixLength == 0 || textLength < prefixLength) {
        return -1;
    }

    int i = 0;
    while (i < textLength) {
        // Skip non-word characters
        while (i < textLength && !Character.isLetterOrDigit(text.charAt(i))) {
            i++;
        }

        if (i + prefixLength > textLength) {
            return -1;
        }

        // Compare the prefixes
        int j;
        for (j = 0; j < prefixLength; j++) {
            if (Character.toUpperCase(text.charAt(i + j)) != prefix.charAt(j)) {
                break;
            }
        }
        if (j == prefixLength) {
            return i;
        }

        // Skip this word
        while (i < textLength && Character.isLetterOrDigit(text.charAt(i))) {
            i++;
        }
    }

    return -1;
}

From source file:com.qatickets.common.StringHelper.java

public static String leaveOnlyLettersAndDigits(String value) {
    StringBuilder sb = new StringBuilder();
    if (StringUtils.isNotBlank(value)) {
        char[] chars = value.toCharArray();
        for (char ch : chars) {
            if (Character.isLetterOrDigit(ch)) {
                sb.append(ch);//from  w w w.j  a  va2s  .c o m
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Checks that the first letter in the string is a letter and not a digit.
 * If this is true is returns the untouched properyName
 * <p/>/*  w  w  w .j  a v a2s.co  m*/
 * If it starts with something other then a letter then it prepends an 'n'
 * character to the front of the string.
 * 
 * @param propertyName
 * @return a property name that started with 'n' is it starts with something
 *         other then a letter.
 */

public static String getCleanPropertyName(String propertyName) {
    StringBuilder sb = new StringBuilder();
    if (propertyName != null && propertyName.length() > 0) {
        char firstValue = propertyName.charAt(0);
        if (Character.isDigit(firstValue) || !Character.isLetter(firstValue)) {
            // propertyName = "n" + propertyName;
            sb.append('n');
        }
        for (int i = 0; i < propertyName.length(); i++) {
            char iChar = propertyName.charAt(i);
            if (Character.isLetterOrDigit(iChar) || iChar == '.' || iChar == '-' || iChar == '_'
                    || iChar == ':') {
                sb.append(iChar);
            }
        }
    }

    return sb.toString();

}

From source file:com.google.flightmap.parsing.util.StringUtils.java

/**
 * Returns mixed-case version of {@code text}.
 * <p>//ww w .  j  av  a  2 s .  c om
 * The first letter of each word is capitalized, the rest are lower case.
 * Words are delimited by spaces and special characters (except single-quote).
 * "REID-HILLVIEW" becomes "Reid-Hillview".
 * 
 * @return mixed-case version of {@code text} with each word capitalized.
 */
public static String capitalize(final String text) {
    final StringBuilder sb = new StringBuilder(WordUtils.capitalize(text.toLowerCase()));
    boolean makeNextLetterUpper = false;
    for (int i = 0; i < sb.length(); ++i) {
        final char cur = sb.charAt(i);
        if (Character.isWhitespace(cur)) {
            continue; // Skip whitespace
        } else if (Character.isLetterOrDigit(cur)) {
            if (makeNextLetterUpper) {
                sb.setCharAt(i, Character.toUpperCase(cur));
                makeNextLetterUpper = false;
            } else {
                continue; // Skip character if no change is neded
            }
        } else { // Not whitespace, letter or digit:  we assume punctuation.
            makeNextLetterUpper = cur != '\''; // Ignore single quote (John'S, Susie'S, ...)
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Returns a variant of the supplied string with all invalid XML name characters
 * removed. A special case is the the space character, which is converted to the '_'
 * character instead.//w  ww .j a v a  2  s  .com
 * @param name A string that could be used as an XML name.
 * @return A valid XML name equivalent of name.
 */

public static String toValidXMLName(String name) {

    char[] nameAsCharArray = name.toCharArray();
    char[] newNameAsCharArray = new char[nameAsCharArray.length];

    boolean currentCharacterValid = false;
    int j = 0;

    for (int i = 0; i < nameAsCharArray.length; i++) {
        currentCharacterValid = false;

        if (nameAsCharArray[i] == ' ') {
            nameAsCharArray[i] = '_';
        }

        if (Character.isLetterOrDigit(nameAsCharArray[i])) {
            currentCharacterValid = true;
        }

        if (nameAsCharArray[i] == '_' || nameAsCharArray[i] == '-' || nameAsCharArray[i] == '.') {
            currentCharacterValid = true;
        }

        if (currentCharacterValid) {
            newNameAsCharArray[j] = nameAsCharArray[i];
            j++;
        }
    }

    String validElementName = new String(newNameAsCharArray);
    validElementName = validElementName.trim();

    return validElementName;
}

From source file:Main.java

/**
 * <p>Checks if the String contains only unicode letters, digits
 * or space (<code>' '</code>).</p>
 *
 * <p><code>null</code> will return <code>false</code>.
 * An empty String ("") will return <code>true</code>.</p>
 *
 * <pre>/*w w w.j  a va  2s . c  om*/
 * StringUtils.isAlphanumeric(null)   = false
 * StringUtils.isAlphanumeric("")     = true
 * StringUtils.isAlphanumeric("  ")   = true
 * StringUtils.isAlphanumeric("abc")  = true
 * StringUtils.isAlphanumeric("ab c") = true
 * StringUtils.isAlphanumeric("ab2c") = true
 * StringUtils.isAlphanumeric("ab-c") = false
 * </pre>
 *
 * @param str  the String to check, may be null
 * @return <code>true</code> if only contains letters, digits or space,
 *  and is non-null
 */
public static boolean isAlphanumericSpace(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) && (str.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

protected static boolean checkNameChar(char ch) {
    if (Character.isLetterOrDigit(ch))
        return true;
    if (ch == '_')
        return true;
    if (ch == '-')
        return true;
    return false;
}