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:net.sf.jabref.importer.fileformat.BibtexParser.java

/**
 * This method is used to parse the bibtex key for an entry.
 *///from  ww w.j ava 2  s  .  co  m
private String parseKey() throws IOException {
    StringBuilder token = new StringBuilder(20);

    while (true) {
        int character = read();

        if (character == -1) {
            eof = true;

            return token.toString();
        }

        if (!Character.isWhitespace((char) character) && (Character.isLetterOrDigit((char) character)
                || (character == ':') || ("#{}~,=\uFFFD".indexOf(character) == -1))) {
            token.append((char) character);
        } else {

            if (Character.isWhitespace((char) character)) {
                // We have encountered white space instead of the comma at
                // the end of
                // the key. Possibly the comma is missing, so we try to
                // return what we
                // have found, as the key and try to restore the rest in fixKey().
                return token + fixKey();
            } else if ((character == ',') || (character == '}')) {
                unread(character);
                return token.toString();
            } else if (character == '=') {
                // If we find a '=' sign, it is either an error, or
                // the entry lacked a comma signifying the end of the key.
                return token.toString();
            } else {
                throw new IOException("Error in line " + line + ":" + "Character '" + (char) character
                        + "' is not " + "allowed in bibtex keys.");
            }

        }
    }

}

From source file:bfile.util.StringUtils.java

/**
 * <p>Strips any of a set of characters from the end of a String.</p>
 *
 * <p>A {@code null} input String returns {@code null}.
 * An empty string ("") input returns the empty string.</p>
 *
 * <p>If the stripChars String is {@code null}, whitespace is
 * stripped as defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>/* w  w w  .  j a va  2  s  .  c om*/
 * StringUtils.stripEnd(null, *)          = null
 * StringUtils.stripEnd("", *)            = ""
 * StringUtils.stripEnd("abc", "")        = "abc"
 * StringUtils.stripEnd("abc", null)      = "abc"
 * StringUtils.stripEnd("  abc", null)    = "  abc"
 * StringUtils.stripEnd("abc  ", null)    = "abc"
 * StringUtils.stripEnd(" abc ", null)    = " abc"
 * StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
 * StringUtils.stripEnd("120.00", ".0")   = "12"
 * </pre>
 *
 * @param str  the String to remove characters from, may be null
 * @param stripChars  the set of characters to remove, null treated as whitespace
 * @return the stripped String, {@code null} if null String input
 */
public static String stripEnd(final String str, final String stripChars) {
    int end;
    if (str == null || (end = str.length()) == 0) {
        return str;
    }

    if (stripChars == null) {
        while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
            end--;
        }
    } else if (stripChars.isEmpty()) {
        return str;
    } else {
        while (end != 0 && stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND) {
            end--;
        }
    }
    return str.substring(0, end);
}

From source file:com.aliyun.odps.utils.StringUtils.java

public static String stripStart(String str, String stripChars) {
    int strLen;/* www .  ja  v a2 s  . c  o  m*/
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }
    int start = 0;
    if (stripChars == null) {
        while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
            start++;
        }
    } else if (stripChars.length() == 0) {
        return str;
    } else {
        while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {
            start++;
        }
    }
    return str.substring(start);
}

From source file:com.meiah.core.util.StringUtil.java

private static boolean _isTrimable(char c, char[] exceptions) {
     if ((exceptions != null) && (exceptions.length > 0)) {
         for (int i = 0; i < exceptions.length; i++) {
             if (c == exceptions[i]) {
                 return false;
             }/*from  ww w. ja v a2  s  . c  o  m*/
         }
     }

     return Character.isWhitespace(c);
 }

From source file:com.aliyun.odps.utils.StringUtils.java

public static String stripEnd(String str, String stripChars) {
    int end;//from  w ww  .j  a va  2s.com
    if (str == null || (end = str.length()) == 0) {
        return str;
    }

    if (stripChars == null) {
        while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
            end--;
        }
    } else if (stripChars.length() == 0) {
        return str;
    } else {
        while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
            end--;
        }
    }
    return str.substring(0, end);
}

From source file:CharUtils.java

/**
 * True if character is whitespace.// ww  w  . ja  va 2 s .c om
 * 
 * @param c
 *            Character to check for being whitespace.
 * 
 * @return True if character c is whitespace.
 */

public static boolean isWhitespace(char c) {
    return Character.isWhitespace(c);
}

From source file:net.sf.jabref.importer.fileformat.BibtexParser.java

private StringBuffer parseBracketedText() throws IOException {
    StringBuffer value = new StringBuffer();

    consume('{', '(');

    int brackets = 0;

    while (!((isClosingBracketNext()) && (brackets == 0))) {

        int character = read();
        if (isEOFCharacter(character)) {
            throw new IOException("Error in line " + line + ": EOF in mid-string");
        } else if ((character == '{') || (character == '(')) {
            brackets++;//  www.j ava 2  s .  c  o m
        } else if ((character == '}') || (character == ')')) {
            brackets--;
        }

        // If we encounter whitespace of any kind, read it as a
        // simple space, and ignore any others that follow immediately.
        /*
         * if (j == '\n') { if (peek() == '\n') value.append('\n'); } else
         */
        if (Character.isWhitespace((char) character)) {
            String whitespacesReduced = skipAndRecordWhitespace(character);

            if (!(whitespacesReduced.isEmpty()) && !"\n\t".equals(whitespacesReduced)) { // &&
                whitespacesReduced = whitespacesReduced.replace("\t", ""); // Remove tabulators.
                value.append(whitespacesReduced);
            } else {
                value.append(' ');
            }

        } else {
            value.append((char) character);
        }
    }

    consume('}', ')');

    return value;
}

From source file:com.easyjf.util.StringUtils.java

/**
 * Trim leading whitespace from the given String.
 * /*from   w  w w .ja  va 2 s .  c o  m*/
 * @param str
 *            the String to check
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(String str) {
    if (!hasLength(str))
        return str;
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0)))
        buf.deleteCharAt(0);
    return buf.toString();
}

From source file:dk.netarkivet.wayback.batch.copycode.NetarchiveSuiteUURIFactory.java

/**
 * Escape any whitespace found.//  w  ww  . j  ava 2  s.  co  m
 *
 * The parent class takes care of the bulk of escaping.  But if any
 * instance of escaping is found in the URI, then we ask for parent
 * to do NO escaping.  Here we escape any whitespace found irrespective
 * of whether the uri has already been escaped.  We do this for
 * case where uri has been judged already-escaped only, its been
 * incompletly done and whitespace remains.  Spaces, etc., in the URI are
 * a real pain.  Their presence will break log file and ARC parsing.
 * @param uri URI string to check.
 * @return uri with spaces escaped if any found.
 */
protected String escapeWhitespace(String uri) {
    // Just write a new string anyways.  The perl '\s' is not
    // as inclusive as the Character.isWhitespace so there are
    // whitespace characters we could miss.  So, rather than
    // write some awkward regex, just go through the string
    // a character at a time.  Only create buffer first time
    // we find a space.
    MutableString buffer = null;
    for (int i = 0; i < uri.length(); i++) {
        char c = uri.charAt(i);
        if (Character.isWhitespace(c)) {
            if (buffer == null) {
                buffer = new MutableString(uri.length() + 2 /*If space, two extra characters (at least)*/
                );
                buffer.append(uri.substring(0, i));
            }
            buffer.append("%");
            String hexStr = Integer.toHexString(c);
            if ((hexStr.length() % 2) > 0) {
                buffer.append("0");
            }
            buffer.append(hexStr);

        } else {
            if (buffer != null) {
                buffer.append(c);
            }
        }
    }
    return (buffer != null) ? buffer.toString() : uri;
}