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:Main.java

/**
 * <p>Checks whether the <code>String</code> contains only
 * digit characters.</p>/*  w w w .  j a v  a  2  s.c o m*/
 *
 * <p><code>Null</code> and empty String will return
 * <code>false</code>.</p>
 *
 * @param str  the <code>String</code> to check
 * @return <code>true</code> if str contains only unicode numeric
 */
public static boolean isDigits(String str) {
    if (isEmpty(str)) {
        return false;
    }
    for (int i = 0; i < str.length(); i++) {
        if (!Character.isDigit(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public void focusGained(FocusEvent evt) {
    final JTextComponent c = (JTextComponent) evt.getSource();
    String s = c.getText();//w w w. jav  a  2 s  . c om

    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) {
            c.setSelectionStart(i);
            c.setSelectionEnd(i);
            break;
        }
    }
}

From source file:Main.java

/**
 * It decodes the given string// www.j a v  a 2s. c  om
 * 
 * @param encodedURI
 * @return decoded string
 */
public static String decodeURIComponent(final String encodedURI) {
    char actualChar;

    StringBuffer buffer = new StringBuffer();

    int bytePattern, sumb = 0;

    for (int i = 0, more = -1; i < encodedURI.length(); i++) {
        actualChar = encodedURI.charAt(i);

        switch (actualChar) {
        case '%': {
            actualChar = encodedURI.charAt(++i);
            int hb = (Character.isDigit(actualChar) ? actualChar - '0'
                    : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
            actualChar = encodedURI.charAt(++i);
            int lb = (Character.isDigit(actualChar) ? actualChar - '0'
                    : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
            bytePattern = (hb << 4) | lb;
            break;
        }
        case '+': {
            bytePattern = ' ';
            break;
        }
        default: {
            bytePattern = actualChar;
        }
        }

        if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx
            sumb = (sumb << 6) | (bytePattern & 0x3f);
            if (--more == 0) {
                buffer.append((char) sumb);
            }
        } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
            buffer.append((char) bytePattern);
        } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
            sumb = bytePattern & 0x1f;
            more = 1;
        } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
            sumb = bytePattern & 0x0f;
            more = 2;
        } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx
            sumb = bytePattern & 0x07;
            more = 3;
        } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx
            sumb = bytePattern & 0x03;
            more = 4;
        } else { // 1111110x
            sumb = bytePattern & 0x01;
            more = 5;
        }
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * Return the original object if no cleanup is done, or for example "" if there are zero characters worth
 * keeping after cleanup//w  ww.j a va2  s.c  om
 * <p>
 * Cleanup means conformance to "#tagnumberlowercasedigitsnotinfirstposition"
 *
 * @param value
 * @return
 */
@NonNull
public static String cleanupTag(@NonNull final Object value, final char firstCharacter) {
    final String tag = ((String) value);
    String cleanedTag = tag.trim().toLowerCase();

    while (cleanedTag.startsWith("##") || cleanedTag.startsWith("@@")) {
        cleanedTag = cleanedTag.substring(1);
    }
    final StringBuffer sb = new StringBuffer(cleanedTag.length());
    boolean first = true;
    for (char c : cleanedTag.toCharArray()) {
        if (!Character.isLetter(c)) {
            // Is not a letter
            if (!first && Character.isDigit(c)) {
                sb.append(c);
            }
            continue;
        }
        sb.append(c);
        first = false;
    }
    String s = sb.toString();
    if (!s.startsWith("" + firstCharacter)) {
        s = firstCharacter + s;
    }
    if (!s.equals(tag)) {
        return s;
    }

    return tag;
}

From source file:exm.stc.tclbackend.tree.Proc.java

/**
 * Check that there are no invalid characters
 *///from w w w  .j av a2  s. c om
private static void checkTclFunctionName(String name) {
    for (int i = 0; i < name.length(); i++) {
        char c = name.charAt(i);
        if (Character.isLetter(c) || Character.isDigit(c) || c == ':' || c == '_' || c == '=' || c == '-'
                || c == '<' || c == '>') {
            // Whitelist of characters
        } else {
            throw new STCRuntimeError("Bad character '" + c + "' in tcl function name " + name);
        }
    }
}

From source file:Main.java

/**
 * Compare two string by chuning digits and non-digits separately and compare between the two.
 * This way, the version strings (Ex: 10.2c.4.24546 sp1) can be properly compared.
 *
 * @param str1//w w  w .j  av a2 s.c o  m
 * @param str2
 * @return
 */
public static int alnumCompare(String str1, String str2) {
    // Ensure null cases are handled for both s1 and s2.
    if (str1 == str2) {
        return 0;
    } else if (str1 == null) {
        return -1;
    } else if (str2 == null) {
        return 1;
    }
    int s1Length = str1.length();
    int s2Length = str2.length();

    for (int s1Index = 0, s2Index = 0; s1Index < s1Length && s2Index < s2Length;) {
        String thisStr = getDigitOrNonDigitChunk(str1, s1Length, s1Index);
        s1Index += thisStr.length();

        String thatStr = getDigitOrNonDigitChunk(str2, s2Length, s2Index);
        s2Index += thatStr.length();

        // If both strs contain numeric characters, sort them numerically
        int result = 0;
        if (Character.isDigit(thisStr.charAt(0)) && Character.isDigit(thatStr.charAt(0))) {
            // Simple str comparison by length.
            int thisStrLength = thisStr.length();
            result = thisStrLength - thatStr.length();
            // If equal, the first different number counts
            if (result == 0) {
                for (int i = 0; i < thisStrLength; i++) {
                    result = thisStr.charAt(i) - thatStr.charAt(i);
                    if (result != 0) {
                        return result;
                    }
                }
            }
        } else {
            result = thisStr.compareToIgnoreCase(thatStr);
        }

        if (result != 0) {
            return result;
        }
    }
    return s1Length - s2Length;
}

From source file:com.hs.mail.imap.schedule.ScheduleUtils.java

public static Date getTimeAfter(String str, Date defaultTime) {
    if (str != null) {
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            char ch = str.charAt(i);
            if (!Character.isDigit(ch)) {
                try {
                    int amount = Integer.parseInt(str.substring(0, i));
                    switch (Character.toUpperCase(ch)) {
                    case 'H':
                        return DateUtils.addHours(new Date(), amount);
                    case 'M':
                        return DateUtils.addMinutes(new Date(), amount);
                    }/* w w w  . ja v  a  2s  .  co  m*/
                } catch (NumberFormatException e) {
                    break;
                }
            }
        }
    }
    return defaultTime;
}

From source file:Main.java

@Override
public void insertString(DocumentFilter.FilterBypass fp, int offset, String string, AttributeSet aset)
        throws BadLocationException {
    int len = string.length();
    boolean isValidInteger = true;

    for (int i = 0; i < len; i++) {
        if (!Character.isDigit(string.charAt(i))) {
            isValidInteger = false;/*from  w  ww. ja va 2s . c o m*/
            break;
        }
    }
    if (isValidInteger) {
        super.insertString(fp, offset, string, aset);
    } else {
        System.out.println("not valid integer");
    }
}

From source file:ch.cyberduck.core.AlphanumericRandomStringService.java

@Override
public String random() {
    return new RandomStringGenerator.Builder().withinRange('0', 'z').filteredBy(new CharacterPredicate() {
        @Override//from w ww .ja v a 2 s .  c o  m
        public boolean test(final int codePoint) {
            return Character.isAlphabetic(codePoint) || Character.isDigit(codePoint);
        }
    }).build().generate(8);
}

From source file:demo.OwaspCharacterEscapes.java

public OwaspCharacterEscapes() {
    ESCAPES = standardAsciiEscapesForJSON();
    for (int i = 0; i < ESCAPES.length; i++) {
        if (!(Character.isAlphabetic(i) || Character.isDigit(i))) {
            ESCAPES[i] = CharacterEscapes.ESCAPE_CUSTOM;
        }/*  ww w  . j a  va 2 s  . c  om*/
    }
}