Example usage for java.lang Character isWhitespace

List of usage examples for java.lang Character isWhitespace

Introduction

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

Prototype

public static boolean isWhitespace(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is white space according to Java.

Usage

From source file:com.hp.alm.ali.idea.translate.expr.Lexer.java

Lexeme next() {
    if (next == null) {
        while (pos < str.length() && Character.isWhitespace(str.charAt(pos))) {
            ++pos;/*from  w ww. j  ava 2 s .c om*/
        }
        if (pos > str.length()) {
            throw new NoSuchElementException();
        }
        if (pos == str.length()) {
            next = new Lexeme(pos, pos, "", Type.END);
            return next;
        }
        int startPos = pos;
        if (str.charAt(pos) == '\'' || str.charAt(pos) == '"') {
            int end = pos;
            do {
                ++end;
            } while (end < str.length() && str.charAt(end) != str.charAt(pos));
            if (end == str.length() || str.charAt(end) != str.charAt(pos)) {
                throw new ParserException("Unterminated literal", pos);
            }
            pos = end + 1;
            next = new Lexeme(startPos, end, str.substring(startPos + 1, end), Type.VALUE);
            return next;
        }
        for (Type type : Type.values()) {
            if (type.getRepr() == null) {
                continue;
            }

            if (str.substring(pos).toUpperCase().startsWith(type.getRepr())) {
                int len = type.getRepr().length();
                if (CharUtils.isAsciiAlphanumeric(type.getRepr().charAt(len - 1)) && str.length() > pos + len
                        && CharUtils.isAsciiAlphanumeric(str.charAt(pos + len))) {
                    // don't split words to match lexeme
                    continue;
                }
                next = new Lexeme(startPos, startPos + len, str.substring(startPos, startPos + len), type);
                pos += len;
                return next;
            }
        }
        do {
            ++pos;
        } while (pos < str.length() && allowedInLiteral(str.charAt(pos)));
        next = new Lexeme(startPos, pos, str.substring(startPos, pos), Type.VALUE);
    }
    return next;
}

From source file:StringUtils.java

/**
 * Check whether the given String contains any whitespace characters.
 * @param str the String to check (may be <code>null</code>)
 * @return <code>true</code> if the String is not empty and
 * contains at least 1 whitespace character
 * @see java.lang.Character#isWhitespace
 *///from   w  w w.ja  v a2 s  . c  o m
public static boolean containsWhitespace(String str) {
    if (!hasLength(str)) {
        return false;
    }
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:com.yahoo.semsearch.fastlinking.utils.Normalize.java

License:asdf

/**
 * Normalizes and returns a span array//from   w w w  .j a  va2s.  c o  m
 * @param args string to normalize
 * @return processed span array with (normalized) string chunks
 */
public static Span[] normalizeWithSpans(String args) {
    final StringBuilder t = new StringBuilder();
    final int length = args.length();
    List<Span> res = new ArrayList<Span>();
    int pi = -1;
    for (int i = 0; i < length; i++) {
        char charAt = args.charAt(i);
        if (Character.isLetterOrDigit(charAt) && !Character.isWhitespace(charAt)) {
            if (pi < 0)
                pi = i;
            t.append(Character.toLowerCase(charAt));
        } else {
            if (t.length() > 0) {
                res.add(new Span(t.toString(), pi, i));
                pi = -1;
                t.setLength(0);
            }
        }
    }
    if (t.length() > 0)
        res.add(new Span(t.toString(), pi, length));
    return res.toArray(new Span[0]);
}

From source file:Main.java

/**
 * <p>//from  w w w  .  j  a v  a2  s.c om
 * Swaps the case of String.
 * </p>
 * <p/>
 * <p>
 * Properly looks after making sure the start of words are Titlecase and not Uppercase.
 * </p>
 * <p/>
 * <p>
 * <code>null</code> is returned as <code>null</code>.
 * </p>
 *
 * @param str the String to swap the case of
 * @return the modified String
 */
public static String swapCase(String str) {
    if (str == null) {
        return null;
    }
    int sz = str.length();
    StringBuilder buffer = new StringBuilder(sz);

    boolean whitespace = false;
    char ch;
    char tmp;

    for (int i = 0; i < sz; i++) {
        ch = str.charAt(i);
        if (Character.isUpperCase(ch)) {
            tmp = Character.toLowerCase(ch);
        } else if (Character.isTitleCase(ch)) {
            tmp = Character.toLowerCase(ch);
        } else if (Character.isLowerCase(ch)) {
            if (whitespace) {
                tmp = Character.toTitleCase(ch);
            } else {
                tmp = Character.toUpperCase(ch);
            }
        } else {
            tmp = ch;
        }
        buffer.append(tmp);
        whitespace = Character.isWhitespace(ch);
    }
    return buffer.toString();
}

From source file:Main.java

public static String fillTextBox(TextPaint paint, int fragmentWidth, String source) {
    StringBuilder sb = new StringBuilder();
    final int length = source.length();
    // Display whole words only
    int lastWhiteSpace = 0;

    for (int index = 0; paint.measureText(sb.toString()) < fragmentWidth && index < length; index++) {
        char c = source.charAt(index);
        if (Character.isWhitespace(c))
            lastWhiteSpace = index;//  w w  w  . j a v a 2s  . c  o  m
        sb.append(c);
    }

    if (sb.length() != length) {
        // Delete last word part
        sb.delete(lastWhiteSpace, sb.length());
        sb.append("...");
    }

    return sb.toString();

}

From source file:com.cinchapi.concourse.shell.SyntaxTools.java

/**
 * Check {@code line} to see if it is a function call that is missing any
 * commas among arguments.// www .  j  a v  a 2 s.c  o m
 * 
 * @param line
 * @param methods
 * @return the line with appropriate argument commas
 */
public static String handleMissingArgCommas(String line, List<String> methods) {
    int hashCode = methods.hashCode();
    Set<String> hashedMethods = CACHED_METHODS.get(hashCode);
    if (hashedMethods == null) {
        hashedMethods = Sets.newHashSetWithExpectedSize(methods.size());
        hashedMethods.addAll(methods);
        CACHED_METHODS.put(hashCode, hashedMethods);
    }
    char[] chars = line.toCharArray();
    StringBuilder transformed = new StringBuilder();
    StringBuilder gather = new StringBuilder();
    boolean foundMethod = false;
    boolean inSingleQuotes = false;
    boolean inDoubleQuotes = false;
    int parenCount = 0;
    for (char c : chars) {
        if (Character.isWhitespace(c) && !foundMethod) {
            transformed.append(gather);
            transformed.append(c);
            foundMethod = hashedMethods.contains(gather.toString());
            gather.setLength(0);
        } else if (Character.isWhitespace(c) && foundMethod) {
            if (transformed.charAt(transformed.length() - 1) != ',' && !inSingleQuotes && !inDoubleQuotes
                    && c != '\n') {
                transformed.append(",");
            }
            transformed.append(c);
        } else if (c == '(' && !foundMethod) {
            parenCount++;
            transformed.append(gather);
            transformed.append(c);
            foundMethod = hashedMethods.contains(gather.toString());
            gather.setLength(0);
        } else if (c == '(' && foundMethod) {
            parenCount++;
            transformed.append(c);
        } else if (c == ';') {
            transformed.append(c);
            foundMethod = false;
            parenCount = 0;
        } else if (c == ')') {
            parenCount--;
            transformed.append(c);
            foundMethod = parenCount == 0 ? false : foundMethod;
        } else if (c == '"') {
            transformed.append(c);
            inSingleQuotes = !inSingleQuotes;
        } else if (c == '\'') {
            transformed.append(c);
            inDoubleQuotes = !inDoubleQuotes;
        } else if (foundMethod) {
            transformed.append(c);
        } else {
            gather.append(c);
        }
    }
    transformed.append(gather);
    return transformed.toString();
}

From source file:HtmlUtil.java

/**
 * Returns tag's name. Given string represents a HTML body and given starting index
 * <b>must</b> be the index of tag's start (i.e. '<').
 * <p>/*from  www.  j  av  a 2s. c om*/
 *
 * Names of ending tags will always start with '/' character.
 *
 * @param body   hmtl body
 * @param i      index of tag's start
 *
 * @return tag's name, or <code>null</code> if tag not found
 */
public static String getTagName(String body, int i) {
    if (body == null) {
        return null;
    }

    if (body.charAt(i) != '<') {
        return null; // no tag
    }

    int start = i + 1; // skip '<'
    int len = body.length();
    boolean isEndTag = false;

    // skip all non-letters
    while (start < len) {
        char c = body.charAt(start);

        if (c == '>') {
            return null; // tag end found => name not found
        }

        if (c == '/') { // this is an end tag
            start++;
            isEndTag = true;

            continue;
        }

        if (!Character.isWhitespace(c)) {
            break;
        }

        start++;
    }

    if (start == len) {
        return null; // tag name not found
    }

    int end = start;

    // skip all letters
    while (end < len) {
        char c = body.charAt(end);

        if (Character.isWhitespace(c) || (c == '>')) {
            break;
        }

        end++;
    }

    if (end == len) {
        return null; // tag end not found
    }

    String tagName = body.substring(start, end);

    if (isEndTag) {
        tagName = "/" + tagName;
    }

    return tagName;
}

From source file:Main.java

/**
 * <p>/*from ww w  . j  a va  2  s .  c o m*/
 * Checks if the String contains only whitespace.
 * </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 whitespace, and is non-null
 */
public static boolean isWhitespace(String str) {
    if (str == null) {
        return false;
    }
    int sz = str.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isWhitespace(str.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

From source file:com.burkeware.search.api.util.StringUtil.java

/**
 * <pre>/*from  w  ww  . j av a2  s. co  m*/
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param str the String to check, may be null
 * @return <code>true</code> if the String is null, empty or whitespace
 */
public static boolean isBlank(final String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0)
        return true;
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i)))
            return false;
    }
    return true;
}

From source file:StringUtils.java

/**
 * @param stripChars if null, remove leading unicode whitespaces.
 *///from   w ww  .  j  a  v  a2  s  .  c o  m
public static CharSequence stripEnd(CharSequence src, String stripChars) {
    int end;
    if (src == null || (end = src.length()) == 0) {
        return src;
    }
    if (stripChars == null) {
        while ((end != 0) && Character.isWhitespace(src.charAt(end - 1))) {
            end--;
        }
    } else if (stripChars.length() == 0) {
        return src;
    } else {
        while ((end != 0) && (stripChars.indexOf(src.charAt(end - 1)) != -1)) {
            end--;
        }
    }
    return src.subSequence(0, end);
}