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

/**
 * <p>Strips any of a set of characters from the end of a String.</p>
 *
 * <p>A <code>null</code> input String returns <code>null</code>.
 * An empty string ("") input returns the empty string.</p>
 *
 * <p>If the stripChars String is <code>null</code>, whitespace is
 * stripped as defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>/* w  w  w .ja  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"
 * </pre>
 *
 * @param str  the String to remove characters from, may be null
 * @param stripChars  the characters to remove, null treated as whitespace
 * @return the stripped String, <code>null</code> if null String input
 */
public static String stripEnd(String str, 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.length() == 0) {
        return str;
    } else {
        while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
            end--;
        }
    }
    return str.substring(0, end);
}

From source file:com.appglu.impl.util.StringUtils.java

public static boolean isNotEmpty(CharSequence str) {
    if (!hasLength(str)) {
        return false;
    }/*from   w w w.  j  a v  a  2 s.c  o  m*/
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:TextUtilities.java

/**
 * Trims the specified string on the left only.
 *//*from   ww w  .j a va2  s.  c  o m*/

public static String trimLeft(String s) {
    int i = 0;
    while (Character.isWhitespace(s.charAt(i)))
        i++;

    return s.substring(i);
}

From source file:com.googlecode.l10nmavenplugin.validators.property.TrailingWhitespaceValidator.java

/**
 * Warn if resource ends with any Java whitespace char (" ", "\t", "\n", ...)
 * /*from www. j  a  v  a2  s .  co  m*/
 * @see Character#isWhitespace
 */
public int validate(Property property, List<L10nReportItem> reportItems) {
    String message = property.getMessage();
    if (message.length() > 0) {
        Character tail = message.charAt(message.length() - 1);
        if (Character.isWhitespace(tail)) {
            L10nReportItem reportItem = new L10nReportItem(Type.TRAILING_WHITESPACE,
                    "Resource ends with whitespace character [" + StringEscapeUtils.escapeJava(tail.toString())
                            + "] which may indicate some resources concatenation",
                    property, null);
            reportItems.add(reportItem);
            logger.log(reportItem);
        }
    }
    return 0;
}

From source file:StringUtils.java

/**
 * Returns the index of the first whitespace character or '-' in <var>line</var>
 * that is at or before <var>start</var>. Returns -1 if no such character is
 * found.//  www.  java 2s.c  o m
 * 
 * @param line
 *          a string
 * @param start
 *          where to star looking
 */
public static int findBreakBefore(String line, int start) {
    for (int i = start; i >= 0; --i) {
        char c = line.charAt(i);
        if (Character.isWhitespace(c) || c == '-')
            return i;
    }
    return -1;
}

From source file:com.github.rvesse.airline.parser.options.MaybePairValueOptionParser.java

public MaybePairValueOptionParser(char separator) {
    if (Character.isWhitespace(separator))
        throw new IllegalArgumentException("Pair separator character cannot be a whitespace character");
    this.separator = separator;
}

From source file:com.easemob.dataexport.utils.StringUtils.java

public static String compactWhitespace(String str) {
    if (str == null) {
        return null;
    }//  w w  w  .  j av  a2  s.c  om
    boolean prevWS = false;
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (Character.isWhitespace(c)) {
            if (!prevWS) {
                builder.append(' ');
            }
            prevWS = true;
        } else {
            prevWS = false;
            builder.append(c);
        }
    }
    return builder.toString().trim();
}

From source file:com.basetechnology.s0.agentserver.util.XmlUtils.java

public char getNonBlankChar() {
    char ch = getChar();
    while (Character.isWhitespace(ch))
        ch = getNextChar();//  w w  w  . j  a v a2  s .c o  m
    return ch;
}

From source file:com.github.rvesse.airline.parser.options.ListValueOptionParser.java

public ListValueOptionParser(char separator) {
    if (Character.isWhitespace(separator))
        throw new IllegalArgumentException("List separator character cannot be a whitespace character");
    this.separator = separator;
}

From source file:net.dv8tion.jda.core.requests.Response.java

protected Response(final okhttp3.Response response, final int code, final String message, final long retryAfter,
        final Set<String> cfRays) {
    this.rawResponse = response;
    this.code = code;
    this.message = message;
    this.exception = null;
    this.retryAfter = retryAfter;
    this.cfRays = cfRays;

    if (response == null || response.body().contentLength() == 0) {
        this.object = null;
        return;//w w w. j a  v  a 2 s  .c  om
    }

    InputStream body = null;
    BufferedReader reader = null;
    try {
        body = Requester.getBody(response);
        // this doesn't add overhead as org.json would do that itself otherwise
        reader = new BufferedReader(new InputStreamReader(body));
        char begin; // not sure if I really like this... but we somehow have to get if this is an object or an array
        int mark = 1;
        do {
            reader.mark(mark++);
            begin = (char) reader.read();
        } while (Character.isWhitespace(begin));

        reader.reset();

        if (begin == '{')
            this.object = new JSONObject(new JSONTokener(reader));
        else if (begin == '[')
            this.object = new JSONArray(new JSONTokener(reader));
        else
            this.object = reader.lines().collect(Collectors.joining());
    } catch (final Exception e) {
        throw new RuntimeException("An error occurred while parsing the response for a RestAction", e);
    } finally {
        try {
            body.close();
            reader.close();
        } catch (NullPointerException | IOException ignored) {
        }
    }
}