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

/**
 * Trim trailing whitespace from the given String.
 * @param str the String to check/*from   ww w.j av  a2s  . c o m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Trim leading and trailing whitespace from the given String.
 * @param str the String to check/* w  ww . j a  v a2  s  .c o m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
        sb.deleteCharAt(0);
    }
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:Main.java

public static String escape(String value) {
    boolean wrapWithQuote = false;
    // +8 is a spare padding for quote expansion
    int len = value.length();
    StringBuffer buf = new StringBuffer(len + 8);
    int i;//from   w w  w .  j a  v a2 s  . c  om
    char c;

    if (len == 0)
        return "\"\"";
    if (Character.isWhitespace(value.charAt(0)))
        wrapWithQuote = true;
    else if (Character.isWhitespace(value.charAt(len - 1)))
        wrapWithQuote = true;

    for (i = 0; i < len; i++) {
        c = value.charAt(i);

        switch (c) {
        case ',':
        case '=':
            wrapWithQuote = true;
            buf.append(c);
            break;
        case '"':
            wrapWithQuote = true;
            buf.append("\"\"");
            break;
        default:
            buf.append(c);
        }
    }

    if (wrapWithQuote) {
        return "\"" + buf.toString() + '"';
    } else {
        return buf.toString();
    }
}

From source file:com.livinglogic.ul4.BoundStringMethodRSplit.java

public static List<String> call(String object, int maxsplit) {
    ArrayList<String> result = new ArrayList<String>();
    int start, end;
    start = end = object.length() - 1;/*from www .j  a  va 2  s . co  m*/
    while (maxsplit-- > 0) {
        while (start >= 0 && Character.isWhitespace(object.charAt(start)))
            --start;
        if (start < 0)
            break;
        end = start--;
        while (start >= 0 && !Character.isWhitespace(object.charAt(start)))
            --start;
        if (start != end)
            result.add(0, object.substring(start + 1, end + 1));
    }
    if (start >= 0) {
        while (start >= 0 && Character.isWhitespace(object.charAt(start)))
            --start;
        if (start >= 0)
            result.add(0, object.substring(0, start + 1));
    }
    return result;
}

From source file:Main.java

private static final String capitalize(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }/*from  w w  w  .  java2 s .com*/
    final char[] arr = str.toCharArray();
    boolean capitalizeNext = true;
    String phrase = "";
    for (final char c : arr) {
        if (capitalizeNext && Character.isLetter(c)) {
            phrase += Character.toUpperCase(c);
            capitalizeNext = false;
            continue;
        } else if (Character.isWhitespace(c)) {
            capitalizeNext = true;
        }
        phrase += c;
    }
    return phrase;
}

From source file:Main.java

/**
 * Check whether the given String has actual text.
 * More specifically, returns <code>true</code> if the string not <code>null</code>,
 * its length is greater than 0, and it contains at least one non-whitespace character.
 * <p/>//from w  w w. j  a  v a 2 s.  c o m
 * <code>StringUtils.hasText(null) == false<br/>
 * StringUtils.hasText("") == false<br/>
 * StringUtils.hasText(" ") == false<br/>
 * StringUtils.hasText("12345") == true<br/>
 * StringUtils.hasText(" 12345 ") == true</code>
 * <p/>
 * <p>Copied from the Spring Framework while retaining all license, copyright and author information.
 *
 * @param str the String to check (may be <code>null</code>)
 * @return <code>true</code> if the String is not <code>null</code>, its length is
 * greater than 0, and it does not contain whitespace only
 * @see Character#isWhitespace
 */
public static boolean hasText(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:Main.java

public static boolean isEmpty(String str) {
    if (str == null) {
        return true;
    }/*  ww w  .  j av  a  2  s  .co m*/
    for (int i = 0, length = str.length(); i < length; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Trim <i>all</i> whitespace from the given String:
 * leading, trailing, and in between characters.
 * @param str the String to check/* ww w. j  av  a  2 s.  co  m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimAllWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    int index = 0;
    while (sb.length() > index) {
        if (Character.isWhitespace(sb.charAt(index))) {
            sb.deleteCharAt(index);
        } else {
            index++;
        }
    }
    return sb.toString();
}

From source file:Main.java

private static String[] parseKeyValue(String source, int beginIndex, int endIndex) {
    int point = -1;
    for (int i = beginIndex; i <= endIndex; i++) {
        char c = source.charAt(i);
        if (c == '=' || c == ':') {
            point = i;/*from  w  ww.ja v  a  2  s .com*/
            break;
        }
    }
    if (point == -1) {
        return new String[] { source.substring(beginIndex, endIndex + 1), "" };
    }
    int lp = point - 1;
    int rp = point + 1;
    while (lp > beginIndex && Character.isWhitespace(source.charAt(lp)))
        lp--;
    while (rp < endIndex && Character.isWhitespace(source.charAt(rp)))
        rp++;
    return new String[] { point == beginIndex ? "" : source.substring(beginIndex, lp + 1),
            point == endIndex ? "" : source.substring(rp, endIndex + 1) };
}

From source file:Main.java

private static String getTagName(String buffer) {
    StringBuffer tagName = new StringBuffer();
    // The tag name is every non-whitespace character in the buffer until
    // a whitespace character is encountered
    for (int i = 0; i < buffer.length(); i++) {
        char character = buffer.charAt(i);
        if (Character.isWhitespace(character)) {
            break;
        }// w w w  . ja va  2s .c om
        tagName.append(character);
    }
    return tagName.toString();
}