Example usage for java.lang Character isDigit

List of usage examples for java.lang Character isDigit

Introduction

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

Prototype

public static boolean isDigit(int codePoint) 

Source Link

Document

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

Usage

From source file:MiscUtils.java

/**
* Compares two strings.<p>//  ww  w  . ja  va2  s. c om
* <p/>
* Unlike <function>String.compareTo()</function>,
* this method correctly recognizes and handles embedded numbers.
* For example, it places "My file 2" before "My file 10".<p>
*
* @param str1       The first string
* @param str2       The second string
* @param ignoreCase If true, case will be ignored
* @return negative If str1 &lt; str2, 0 if both are the same,
*         positive if str1 &gt; str2
* @since jEdit 4.3pre5
*/
public static int compareStrings(String str1, String str2, boolean ignoreCase) {
    char[] char1 = str1.toCharArray();
    char[] char2 = str2.toCharArray();

    int len = Math.min(char1.length, char2.length);

    for (int i = 0, j = 0; i < len && j < len; i++, j++) {
        char ch1 = char1[i];
        char ch2 = char2[j];
        if (Character.isDigit(ch1) && Character.isDigit(ch2) && ch1 != '0' && ch2 != '0') {
            int _i = i + 1;
            int _j = j + 1;

            for (; _i < char1.length; _i++) {
                if (!Character.isDigit(char1[_i])) {
                    //_i--;
                    break;
                }
            }

            for (; _j < char2.length; _j++) {
                if (!Character.isDigit(char2[_j])) {
                    //_j--;
                    break;
                }
            }

            int len1 = _i - i;
            int len2 = _j - j;
            if (len1 > len2)
                return 1;
            else if (len1 < len2)
                return -1;
            else {
                for (int k = 0; k < len1; k++) {
                    ch1 = char1[i + k];
                    ch2 = char2[j + k];
                    if (ch1 != ch2)
                        return ch1 - ch2;
                }
            }

            i = _i - 1;
            j = _j - 1;
        } else {
            if (ignoreCase) {
                ch1 = Character.toLowerCase(ch1);
                ch2 = Character.toLowerCase(ch2);
            }

            if (ch1 != ch2)
                return ch1 - ch2;
        }
    }

    return char1.length - char2.length;
}

From source file:Main.java

public static boolean isXMLName(String text) {
    if (text == null || text.length() <= 0)
        return false;
    if (!Character.isLetter(text.charAt(0)))
        return false;
    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        if (Character.isLetter(ch))
            continue;
        if (Character.isDigit(ch))
            continue;
        if (ch == '.' || ch == ':' || ch == '_' || ch == '-')
            continue;
        return false;
    }/*w w  w  .j  a v a2 s  .com*/
    return true;
}

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/>//from w w w  .j  a v  a 2  s  .  c o 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.cognifide.qa.bb.aem.touch.util.DataPathUtil.java

/**
 * Method normalizes data path. Checks if last character of path is digit, if yes, then returns substring
 * before last {@code _} occurrence.//from  w  w  w. jav a2 s.  c o  m
 * Example: for given in parameter string '/title_1234' method will return '/title'
 *
 * @param dataPath data path.
 * @return normalized data path.
 */
public static String normalize(String dataPath) {
    return Character.isDigit(dataPath.charAt(dataPath.length() - 1))
            ? StringUtils.substringBeforeLast(dataPath, "_")
            : dataPath;
}

From source file:Util.java

public static String removeNonDigits(String aString) {
    StringBuffer newString = new StringBuffer();
    for (int x = 0; x < aString.length(); x++) {
        if (Character.isDigit(aString.charAt(x)))
            newString.append(aString.charAt(x));
    }/*from ww  w.  j a  va 2  s.  c  o  m*/
    return (newString.toString());
}

From source file:Main.java

/**
 * Return true if an element/attribute name is a valid NCName (Non Colon Name).
 * /*from   ww  w .  j a  va2s.  com*/
 * @param name Non-qualified (no colon) name for an element/attribute.
 * @return True if an element/attribute name is a valid NCName.
 */
public static boolean isValidNCName(String name) {
    if (name == null || name.length() <= 0)
        return false;
    if (name.equalsIgnoreCase("XML"))
        return false;
    char c = name.charAt(0);
    if (!Character.isLetter(c) && c != '_')
        return false;
    for (int i = 1; i < name.length(); i++) {
        c = name.charAt(i);
        if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_' && c != '.' && c != '-')
            return false;
    }
    return true;
}

From source file:Main.java

/**
 * Escapes the contents a string to be used as a safe scheme name in the URI according to
 * http://tools.ietf.org/html/rfc3986#section-3.1
 *
 * This does not ensure that the first character is a letter (which is required by the RFC).
 *//*from  w  w w  .j  av a2s.co m*/
@VisibleForTesting
public static String escapeForSchemeName(String s) {
    // According to the RFC, scheme names are case-insensitive.
    s = s.toLowerCase();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (Character.isLetter(c) || Character.isDigit(c) || ('-' == c) || ('.' == c)) {
            // Safe - use as is.
            sb.append(c);
        } else if ('+' == c) {
            // + is used as our escape character, so double it up.
            sb.append("++");
        } else {
            // Unsafe - escape.
            sb.append('+').append((int) c);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static boolean isNumber(String s) {
    boolean pointcanappear = true;
    boolean signcanappear = true;
    boolean res = true;
    int i = 0;/*from  ww w .ja  v  a2  s  . c o m*/

    if (s.length() == 0)
        res = false;
    while (i < s.length() && res) {
        char c = s.charAt(i);
        boolean ok = false;
        if (Character.isWhitespace(c))
            ok = true;
        else if (Character.isDigit(c)) {
            if (signcanappear)
                signcanappear = false;
            ok = true;
        } else if (c == '-' && signcanappear) {
            signcanappear = false;
            ok = true;
        } else if ((c == '.' || c == ',') && pointcanappear) {
            pointcanappear = false;
            ok = true;
        }
        if (ok)
            i++;
        else
            res = false;
    }
    return res;
}

From source file:Main.java

/**
 * For some reason, can't find this utility method in the java framework.
 * /*www . ja v  a 2s  .c  o  m*/
 * @param sDateTime
 *            an xsd:dateTime string
 * @return an equivalent java.util.Date
 * @throws ParseException
 */
public static Date parseXsdDateTime(String sDateTime) throws ParseException {
    final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    int iDotPosition = NORMAL_IDOT_POSITION;
    if (sDateTime.charAt(0) == '-') {
        iDotPosition = IDOT_POSITION_IFNEG;
    }
    Date result;
    if (sDateTime.length() <= iDotPosition) {
        return format.parse(sDateTime + "Z");
    }

    String millis = null;
    char c = sDateTime.charAt(iDotPosition);
    if (c == '.') {
        // if datetime has milliseconds, separate them
        int eoms = iDotPosition + 1;
        while (Character.isDigit(sDateTime.charAt(eoms))) {
            eoms += 1;
        }
        millis = sDateTime.substring(iDotPosition, eoms);
        sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms);
        c = sDateTime.charAt(iDotPosition);
    }
    if (c == '+' || c == '-') {
        format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition)));
        sDateTime = sDateTime.substring(0, iDotPosition) + "Z";
    } else if (c != 'Z') {
        throw new ParseException("Illegal timezone specification.", iDotPosition);
    }

    result = format.parse(sDateTime);
    if (millis != null) {
        result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS));
    }

    result = offsetDateFromGMT(result);
    return result;
}

From source file:Main.java

public static String createXmlName(String str) {
    StringBuffer result = new StringBuffer();
    boolean skipped = false;
    boolean numbersOnly = true;

    for (int c = 0; c < str.length(); c++) {
        char ch = str.charAt(c);

        if (Character.isLetter(ch) || ch == '_' || ch == '-' || ch == '.') {
            if (skipped)
                result.append(Character.toUpperCase(ch));
            else//from  w ww . j a  v a  2  s  .  c o m
                result.append(ch);
            numbersOnly = false;
            skipped = false;
        } else if (Character.isDigit(ch)) {
            result.append(ch);
            skipped = false;
        } else {
            skipped = true;
        }
    }

    str = result.toString();
    if (numbersOnly && str.length() > 0)
        str = "_" + str;

    return str;
}