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:eu.delving.sip.xml.SourceConverter.java

private boolean moreNumbersThanLetters(String string) {
    int letters = 0, numbers = 0;
    for (char c : string.toCharArray()) {
        if (Character.isLetter(c)) {
            letters++;/*from w ww.  ja v  a 2s  .com*/
        } else if (Character.isDigit(c)) {
            numbers++;
        }
    }
    return numbers > letters;
}

From source file:org.archive.modules.recrawl.wbm.WbmPersistLoadProcessor.java

protected HashMap<String, Object> getLastCrawl(InputStream is) throws IOException {
    // read CDX lines, save most recent (at the end) hash.
    ByteBuffer buffer = ByteBuffer.allocate(32);
    ByteBuffer tsbuffer = ByteBuffer.allocate(14);
    int field = 0;
    int c;//from  w  ww  .  j a  v  a2  s .  c  om
    do {
        c = is.read();
        if (field == 1) {
            // 14-digits timestamp
            tsbuffer.clear();
            while (Character.isDigit(c) && tsbuffer.remaining() > 0) {
                tsbuffer.put((byte) c);
                c = is.read();
            }
            if (c != ' ' || tsbuffer.position() != 14) {
                tsbuffer.clear();
            }
            // fall through to skip the rest
        } else if (field == 5) {
            buffer.clear();
            while ((c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') && buffer.remaining() > 0) {
                buffer.put((byte) c);
                c = is.read();
            }
            if (c != ' ' || buffer.position() != 32) {
                buffer.clear();
            }
            // fall through to skip the rest
        }
        while (true) {
            if (c == -1) {
                break;
            } else if (c == '\n') {
                field = 0;
                break;
            } else if (c == ' ') {
                field++;
                break;
            }
            c = is.read();
        }
    } while (c != -1);

    HashMap<String, Object> info = new HashMap<String, Object>();
    if (buffer.remaining() == 0) {
        info.put(RecrawlAttributeConstants.A_CONTENT_DIGEST, contentDigestScheme + new String(buffer.array()));
    }
    if (tsbuffer.remaining() == 0) {
        try {
            long ts = DateUtils.parse14DigitDate(new String(tsbuffer.array())).getTime();
            // A_TIMESTAMP has been used for sorting history long before A_FETCH_BEGAN_TIME
            // field was introduced. Now FetchHistoryProcessor fails if A_FETCH_BEGAN_TIME is
            // not set. We could stop storing A_TIMESTAMP and sort by A_FETCH_BEGAN_TIME.
            info.put(FetchHistoryHelper.A_TIMESTAMP, ts);
            info.put(CoreAttributeConstants.A_FETCH_BEGAN_TIME, ts);
        } catch (ParseException ex) {
        }
    }
    return info.isEmpty() ? null : info;
}

From source file:com.blocks.framework.utils.date.StringUtil.java

public static boolean isNumeric(String str) {
     if (str == null)
         return false;

     for (int i = 0; i < str.length(); i++) {
         if (!Character.isDigit(str.charAt(i))) {
             return false;
         }/*  ww w.j  ava 2 s .  c  o m*/
     }
     return true;
 }

From source file:com.apporiented.hermesftp.client.FtpTestClient.java

private String getResponse(BufferedReader in) throws IOException {
    StringBuffer sb = new StringBuffer();
    boolean done;
    do {/*w  w  w.  j  a v a 2 s .com*/
        String line = in.readLine();
        sb.append(line).append("\n");
        int idx = 0;
        done = Character.isDigit(line.charAt(idx++)) && Character.isDigit(line.charAt(idx++))
                && Character.isDigit(line.charAt(idx++)) && line.charAt(idx++) == ' ';
    } while (!done);
    return sb.toString().trim();
}

From source file:ca.uhn.fhir.model.primitive.IdDt.java

private static boolean isValidLong(String id) {
    if (StringUtils.isBlank(id)) {
        return false;
    }/* w w w. j a  v  a  2 s .  c om*/
    for (int i = 0; i < id.length(); i++) {
        if (Character.isDigit(id.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:com.microsoft.tfs.util.datetime.LenientDateTimeParser.java

/**
 * Looks for common time zone strings at the end of the given dateTimeString
 * that {@link SimpleDateFormat} can't parse and converts them to equivalent
 * ones it can. For example, "Z" is turned into "UTC".
 *
 * @param dateTimeString//ww  w  .j  a v  a2s. c  o  m
 *        the date time string to parse. If null, null is returned.
 * @return the given dateTimeString with time zones converted to ones
 *         {@link SimpleDateFormat} can parse. Null if the given string was
 *         null.
 */
protected String convertUnsupportedTimeZones(final String dateTimeString) {
    if (dateTimeString == null) {
        return null;
    }

    /*
     * Convert additional UTC zone strings (like "Z" and "z") to "UTC". We
     * consider the dateTimeString to contain a matching zone string if it
     * ends with it, and the character before it is whitespace or numeric.
     * This lets a date time string like "2007-05-02T22:04:12Foobaz" pass
     * unaltered, because "Foobaz" refers to a (hypothetical) time zone name
     * which SimpleDateFormat could parse.
     */
    for (int i = 0; i < additionalUTCTimeZoneStrings.length; i++) {
        final String zoneString = additionalUTCTimeZoneStrings[i];

        if (dateTimeString.endsWith(zoneString)) {
            final int zoneIndex = dateTimeString.lastIndexOf(zoneString);

            // Make sure there's at least one character before the index.
            if (zoneIndex < 1) {
                continue;
            }

            final char previousChracter = dateTimeString.charAt(zoneIndex - 1);

            if (Character.isDigit(previousChracter) || Character.isWhitespace(previousChracter)) {
                // It's a match, replace the string anchored at the end with
                // UTC.
                return dateTimeString.replaceAll(zoneString + "$", "UTC"); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
    }

    return dateTimeString;
}

From source file:HtmlEncoder.java

/**
 *  Do "smart" encodging on a string. This means that valid HTML entities and tags,
 *  Helma macros and HTML comments are passed through unescaped, while
 *  other occurrences of '<', '>' and '&' are encoded to HTML entities.
 *
 *  @param str the string to encode//from  w  ww.j a v  a2  s.c  o m
 *  @param ret the string buffer to encode to
 *  @param paragraphs if true use p tags for paragraphs, otherwise just use br's
 *  @param allowedTags a set containing the names of allowed tags as strings. All other
 *                     tags will be escaped
 */
public final static void encode(String str, StringBuffer ret, boolean paragraphs, Set allowedTags) {
    if (str == null) {
        return;
    }

    int l = str.length();

    // where to insert the <p> tag in case we want to create a paragraph later on
    int paragraphStart = ret.length();

    // what kind of element/text are we leaving and entering?
    // this is one of TEXT|SEMIBLOCK|BLOCK|INTERNAL
    // depending on this information, we decide whether and how to insert
    // paragraphs and line breaks. "entering" a tag means we're at the '<'
    // and exiting means we're at the '>', not that it's a start or close tag.
    byte entering = TEXT;
    byte exiting = TEXT;

    Stack openTags = new Stack();

    // are we currently within a < and a > that consitute some kind of tag?
    // we use tag balancing to know whether we are inside a tag (and should
    // pass things through unchanged) or outside (and should encode stuff).
    boolean insideTag = false;

    // are we inside an HTML tag?
    boolean insideHtmlTag = false;
    boolean insideCloseTag = false;
    byte htmlTagMode = TAG_NAME;

    // if we are inside a <code> tag, we encode everything to make
    // documentation work easier
    boolean insideCodeTag = false;
    boolean insidePreTag = false;

    // are we within a Helma <% macro %> tag? We treat macro tags and
    // comments specially, since we can't rely on tag balancing
    // to know when we leave a macro tag or comment.
    boolean insideMacroTag = false;

    // are we inside an HTML comment?
    boolean insideComment = false;

    // the quotation mark we are in within an HTML or Macro tag, if any
    char htmlQuoteChar = '\u0000';
    char macroQuoteChar = '\u0000';

    // number of newlines met since the last non-whitespace character
    int linebreaks = 0;

    // did we meet a backslash escape?
    boolean escape = false;

    boolean triggerBreak = false;

    for (int i = 0; i < l; i++) {
        char c = str.charAt(i);

        // step one: check if this is the beginning of an HTML tag, comment or
        // Helma macro.
        if (c == '<') {
            if (i < (l - 2)) {
                if (!insideMacroTag && ('%' == str.charAt(i + 1))) {
                    // this is the beginning of a Helma macro tag
                    if (!insideCodeTag) {
                        insideMacroTag = insideTag = true;
                        macroQuoteChar = '\u0000';
                    }
                } else if (('!' == str.charAt(i + 1)) && ('-' == str.charAt(i + 2))) {
                    // the beginning of an HTML comment?
                    if (!insideCodeTag) {
                        insideComment = insideTag = ((i < (l - 3)) && ('-' == str.charAt(i + 3)));
                    }
                } else if (!insideTag) {
                    // check if this is a HTML tag.
                    insideCloseTag = ('/' == str.charAt(i + 1));
                    int tagStart = insideCloseTag ? (i + 2) : (i + 1);
                    int j = tagStart;

                    while ((j < l) && Character.isLetterOrDigit(str.charAt(j)))
                        j++;

                    if ((j > tagStart) && (j < l)) {
                        String tagName = str.substring(tagStart, j).toLowerCase();

                        if ("code".equals(tagName) && insideCloseTag && insideCodeTag) {
                            insideCodeTag = false;
                        }

                        if (((allowedTags == null) || allowedTags.contains(tagName))
                                && allTags.contains(tagName) && !insideCodeTag) {
                            insideHtmlTag = insideTag = true;
                            htmlQuoteChar = '\u0000';
                            htmlTagMode = TAG_NAME;

                            exiting = entering;
                            entering = TEXT;

                            if (internalTags.contains(tagName)) {
                                entering = INTERNAL;
                            } else if (blockTags.contains(tagName)) {
                                entering = BLOCK;
                            } else if (semiBlockTags.contains(tagName)) {
                                entering = paragraphs ? BLOCK : SEMIBLOCK;
                            }

                            if (entering > 0) {
                                triggerBreak = !insidePreTag;
                            }

                            if (insideCloseTag) {
                                int t = openTags.search(tagName);

                                if (t == -1) {
                                    i = j;
                                    insideHtmlTag = insideTag = false;

                                    continue;
                                } else if (t > 1) {
                                    for (int k = 1; k < t; k++) {
                                        Object tag = openTags.pop();
                                        if (!emptyTags.contains(tag)) {
                                            ret.append("</");
                                            ret.append(tag);
                                            ret.append(">");
                                        }
                                    }
                                }

                                openTags.pop();
                            } else {
                                openTags.push(tagName);
                            }

                            if ("code".equals(tagName) && !insideCloseTag) {
                                insideCodeTag = true;
                            }

                            if ("pre".equals(tagName)) {
                                insidePreTag = !insideCloseTag;
                            }
                        }
                    }
                }
            } // if (i < l-2)
        }

        if ((triggerBreak || linebreaks > 0) && !Character.isWhitespace(c)) {

            if (!insideTag) {
                exiting = entering;
                entering = TEXT;
                if (exiting >= SEMIBLOCK) {
                    paragraphStart = ret.length();
                }
            }

            if (entering != INTERNAL && exiting != INTERNAL) {
                int swallowBreaks = 0;
                if (paragraphs && (entering != BLOCK || exiting != BLOCK) && (exiting < BLOCK)
                        && (linebreaks > 1) && paragraphStart < ret.length()) {
                    ret.insert(paragraphStart, "<p>");
                    ret.append("</p>");
                    swallowBreaks = 2;
                }

                // treat entering a SEMIBLOCK as entering a TEXT 
                int _entering = entering == SEMIBLOCK ? TEXT : entering;
                for (int k = linebreaks - 1; k >= 0; k--) {
                    if (k >= swallowBreaks && k >= _entering && k >= exiting) {
                        ret.append("<br />");
                    }
                    ret.append(newLine);
                }
                if (exiting >= SEMIBLOCK || linebreaks > 1) {
                    paragraphStart = ret.length();
                }

            }

            linebreaks = 0;
            triggerBreak = false;
        }

        switch (c) {
        case '<':

            if (insideTag) {
                ret.append('<');
            } else {
                ret.append("&lt;");
            }

            break;

        case '&':

            // check if this is an HTML entity already,
            // in which case we pass it though unchanged
            if ((i < (l - 3)) && !insideCodeTag) {
                // is this a numeric entity?
                if (str.charAt(i + 1) == '#') {
                    int j = i + 2;

                    while ((j < l) && Character.isDigit(str.charAt(j)))
                        j++;

                    if ((j < l) && (str.charAt(j) == ';')) {
                        ret.append("&");

                        break;
                    }
                } else {
                    int j = i + 1;

                    while ((j < l) && Character.isLetterOrDigit(str.charAt(j)))
                        j++;

                    if ((j < l) && (str.charAt(j) == ';')) {
                        ret.append("&");

                        break;
                    }
                }
            }

            // we didn't reach a break, so encode the ampersand as HTML entity
            ret.append("&amp;");

            break;

        case '\\':
            ret.append(c);

            if (insideTag && !insideComment) {
                escape = !escape;
            }

            break;

        case '"':
        case '\'':
            ret.append(c);

            if (!insideComment) {
                // check if the quote is escaped
                if (insideMacroTag) {
                    if (escape) {
                        escape = false;
                    } else if (macroQuoteChar == c) {
                        macroQuoteChar = '\u0000';
                    } else if (macroQuoteChar == '\u0000') {
                        macroQuoteChar = c;
                    }
                } else if (insideHtmlTag) {
                    if (escape) {
                        escape = false;
                    } else if (htmlQuoteChar == c) {
                        htmlQuoteChar = '\u0000';
                        htmlTagMode = TAG_SPACE;
                    } else if (htmlQuoteChar == '\u0000') {
                        htmlQuoteChar = c;
                    }
                }
            }

            break;

        case '\n':
            if (insideTag || insidePreTag) {
                ret.append('\n');
            } else {
                linebreaks++;
            }

            break;
        case '\r':
            if (insideTag || insidePreTag) {
                ret.append('\r');
            }
            break;

        case '>':

            // For Helma macro tags and comments, we overrule tag balancing,
            // i.e. we don't require that '<' and '>' be balanced within
            // macros and comments. Rather, we check for the matching closing tag.
            if (insideComment) {
                ret.append('>');
                insideComment = !((str.charAt(i - 2) == '-') && (str.charAt(i - 1) == '-'));
            } else if (insideMacroTag) {
                ret.append('>');
                insideMacroTag = !((str.charAt(i - 1) == '%') && (macroQuoteChar == '\u0000'));
            } else if (insideHtmlTag) {
                ret.append('>');

                // only leave HTML tag if quotation marks are balanced
                // within that tag.
                insideHtmlTag = htmlQuoteChar != '\u0000';

                // Check if this is an empty tag so we don't generate an
                // additional </close> tag.
                if (str.charAt(i - 1) == '/') {
                    // this is to avoid misinterpreting tags like
                    // <a href=http://foo/> as empty
                    if (htmlTagMode != TAG_ATT_VAL && htmlTagMode != TAG_ATT_NAME) {
                        openTags.pop();
                    }
                }

                exiting = entering;
                if (exiting > 0) {
                    triggerBreak = !insidePreTag;
                }

            } else {
                ret.append("&gt;");
            }

            // check if we still are inside any kind of tag
            insideTag = insideComment || insideMacroTag || insideHtmlTag;
            insideCloseTag = insideTag;

            break;

        default:

            if (insideHtmlTag && !insideCloseTag) {
                switch (htmlTagMode) {
                case TAG_NAME:
                    if (!Character.isLetterOrDigit(c)) {
                        htmlTagMode = TAG_SPACE;
                    }
                    break;
                case TAG_SPACE:
                    if (Character.isLetterOrDigit(c)) {
                        htmlTagMode = TAG_ATT_NAME;
                    }
                    break;
                case TAG_ATT_NAME:
                    if (c == '=') {
                        htmlTagMode = TAG_ATT_VAL;
                    } else if (c == ' ') {
                        htmlTagMode = TAG_SPACE;
                    }
                    break;
                case TAG_ATT_VAL:
                    if (Character.isWhitespace(c) && htmlQuoteChar == '\u0000') {
                        htmlTagMode = TAG_SPACE;
                    }
                    break;
                }
            }
            if (c < 128) {
                ret.append(c);
            } else if ((c >= 128) && (c < 256)) {
                ret.append(transform[c - 128]);
            } else {
                ret.append("&#");
                ret.append((int) c);
                ret.append(";");
            }

            escape = false;
        }
    }

    // if tags were opened but not closed, close them.
    int o = openTags.size();

    if (o > 0) {
        for (int k = 0; k < o; k++) {
            Object tag = openTags.pop();
            if (!emptyTags.contains(tag)) {
                ret.append("</");
                ret.append(tag);
                ret.append(">");
            }
        }
    }

    // add remaining newlines we may have collected
    int swallowBreaks = 0;
    if (paragraphs && entering < BLOCK) {
        ret.insert(paragraphStart, "<p>");
        ret.append("</p>");
        swallowBreaks = 2;
    }

    if (linebreaks > 0) {
        for (int i = linebreaks - 1; i >= 0; i--) {
            if (i >= swallowBreaks && i > exiting) {
                ret.append("<br />");
            }
            ret.append(newLine);
        }
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.editors.RTFDocumentProvider2.java

private ParserPair getNumber(InputStream contentStream, int initial) throws IOException {
    StringBuilder number = new StringBuilder();
    if (initial > -1) {
        number.append(initial);//from  w  w  w.jav  a2 s  .  com
    }
    int c = contentStream.read();
    while (c != -1) {
        char ch = (char) c;
        if (Character.isDigit(ch)) {
            number.append(ch);
            c = contentStream.read();
        } else {
            break;
        }
    }
    return new ParserPair(c, number.toString());
}

From source file:forge.game.card.CardFactoryUtil.java

/**
 * <p>/*ww w  .  jav  a2s  .co  m*/
 * multiplyCost.
 * </p>
 * 
 * @param manacost
 *            a {@link java.lang.String} object.
 * @param multiplier
 *            a int.
 * @return a {@link java.lang.String} object.
 */
public static String multiplyCost(final String manacost, final int multiplier) {
    if (multiplier == 0) {
        return "";
    }
    if (multiplier == 1) {
        return manacost;
    }

    final String[] tokenized = manacost.split("\\s");
    final StringBuilder sb = new StringBuilder();

    if (Character.isDigit(tokenized[0].charAt(0))) {
        // cost starts with "colorless" number cost
        int cost = Integer.parseInt(tokenized[0]);
        cost = multiplier * cost;
        tokenized[0] = "" + cost;
        sb.append(tokenized[0]);
    } else {
        if (tokenized[0].contains("<")) {
            final String[] advCostPart = tokenized[0].split("<");
            final String costVariable = advCostPart[1].split(">")[0];
            final String[] advCostPartValid = costVariable.split("\\/", 2);
            // multiply the number part of the cost object
            int num = Integer.parseInt(advCostPartValid[0]);
            num = multiplier * num;
            tokenized[0] = advCostPart[0] + "<" + num;
            if (advCostPartValid.length > 1) {
                tokenized[0] = tokenized[0] + "/" + advCostPartValid[1];
            }
            tokenized[0] = tokenized[0] + ">";
            sb.append(tokenized[0]);
        } else {
            for (int i = 0; i < multiplier; i++) {
                // tokenized[0] = tokenized[0] + " " + tokenized[0];
                sb.append((" "));
                sb.append(tokenized[0]);
            }
        }
    }

    for (int i = 1; i < tokenized.length; i++) {
        if (tokenized[i].contains("<")) {
            final String[] advCostParts = tokenized[i].split("<");
            final String costVariables = advCostParts[1].split(">")[0];
            final String[] advCostPartsValid = costVariables.split("\\/", 2);
            // multiply the number part of the cost object
            int num = Integer.parseInt(advCostPartsValid[0]);
            num = multiplier * num;
            tokenized[i] = advCostParts[0] + "<" + num;
            if (advCostPartsValid.length > 1) {
                tokenized[i] = tokenized[i] + "/" + advCostPartsValid[1];
            }
            tokenized[i] = tokenized[i] + ">";
            sb.append((" "));
            sb.append(tokenized[i]);
        } else {
            for (int j = 0; j < multiplier; j++) {
                // tokenized[i] = tokenized[i] + " " + tokenized[i];
                sb.append((" "));
                sb.append(tokenized[i]);
            }
        }
    }

    String result = sb.toString();
    System.out.println("result: " + result);
    result = result.trim();
    return result;
}

From source file:com.MainFiles.Functions.java

public String fn_RemoveNon_Numeric(String strExpression) {

    String strtocheck = strExpression.trim();

    char[] myChars = strtocheck.toCharArray();
    String strNumeic = "";

    try {//from  w w w  .j a v  a  2s . com
        // Loop through the array testing if each is a digit

        for (char ch : myChars) {
            if (Character.isDigit(ch)) {
                strNumeic = strNumeic + ch;
            }
        }
        return strNumeic;
    } catch (Exception ex) {
        this.log("\nINFO fn_RemoveNon_Numeric() :: " + ex.getMessage() + "\n" + this.StackTraceWriter(ex),
                "ERROR");
        return "";
    }
}