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

public static void escapedAdd(final StringBuilder sb, final String str) {
    for (int i = 0; i < str.length(); i++) {
        final char ch = str.charAt(i);
        if (ch < 33 || Character.isWhitespace(ch) || Character.isSpaceChar(ch)) {
            sb.append(' ');
        } else {//from   w  ww .  j av a2 s . co  m
            switch (ch) {
            case '"':
                sb.append("&quot;");
                break;
            case '\'':
                sb.append("&apos;");
                break;
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            default:
                sb.append(ch);
                break;
            }
        }
    }
}

From source file:Main.java

/**
 * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
 *
 * <pre>/*  www  .j  a va2  s  .  c  o m*/
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static String toUnicode(String input) {
    StringBuffer ret = new StringBuffer();
    if (input != null) {
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (!Character.isWhitespace(ch) && ch < 0x20 || ch > 0x7e) {
                ret.append("\\u");
                // requires 1.5 VM
                // ret.append(String.format("%1$04x", new Object[] { Integer.valueOf(ch) }));
                ret.append(leading4Zeros(Integer.toHexString(ch)));
            } else {
                ret.append(ch);//ww  w .  j a v  a  2  s  . c om
            }
        }
    }
    return ret.toString();
}

From source file:Main.java

public static String removeWhitespace(String str) {
    if (isEmpty(str)) {
        return str;
    }//from   ww w . j a  va 2  s  . com
    int sz = str.length();
    char[] chs = new char[sz];
    int count = 0;
    for (int i = 0; i < sz; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            chs[count++] = str.charAt(i);
        }
    }
    if (count == sz) {
        return str;
    }

    return new String(chs, 0, count);
}

From source file:Main.java

/***
 * replaces only white spaces from file name
 *//*w  w  w  .j av  a 2  s  .  co m*/
public static String createFileName(String str, char whitespaceChar) {
    StringBuffer result = new StringBuffer();

    for (int c = 0; c < str.length(); c++) {
        char ch = str.charAt(c);

        if (Character.isWhitespace(ch) && whitespaceChar != 0)
            result.append(whitespaceChar);
        else if (Character.isLetterOrDigit(ch))
            result.append(ch);
        else if (ch == whitespaceChar)
            result.append(ch);
    }

    return result.toString();
}

From source file:Main.java

/**
 * <p>Deletes all whitespaces from a String as defined by
 * {@link Character#isWhitespace(char)}.</p>
 * <p>//from   ww  w  .  ja  v  a  2  s .c  o m
 * <pre>
 * StringUtils.deleteWhitespace(null)         = null
 * StringUtils.deleteWhitespace("")           = ""
 * StringUtils.deleteWhitespace("abc")        = "abc"
 * StringUtils.deleteWhitespace("   ab  c  ") = "abc"
 * </pre>
 *
 * @param str the String to delete whitespace from, may be null
 * @return the String without whitespaces, {@code null} if null String input
 */
private static String deleteWhitespace(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    int sz = str.length();
    char[] chs = new char[sz];
    int count = 0;
    for (int i = 0; i < sz; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            chs[count++] = str.charAt(i);
        }
    }
    if (count == sz) {
        return str;
    }
    return new String(chs, 0, count);
}

From source file:Main.java

private static String capitalize(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }//from  w ww  .  j a v  a2  s.  c o m
    char[] arr = str.toCharArray();
    boolean capitalizeNext = true;
    String phrase = "";
    for (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

public static String stripStart(String str, String stripChars) {
    int strLen;//from w  ww .  j a  va  2  s .co  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:Main.java

public static String normalizePI(String contents) {
    if (contents == null || contents.indexOf("?>") >= 0)
        return null;
    int i = 0, len = contents.length();
    for (; i < len; i++)
        if (!Character.isWhitespace(contents.charAt(i)))
            break;
    return (i == 0) ? contents : contents.substring(i);
}

From source file:Main.java

/**
 * //from w  w  w . j  a  v a2s  . c o  m
 * Checks indentation (over a single line - multipline text nodes is not supported)
 * 
 * @param out
 * @param indentSize
 * @return
 * @throws Exception
 */

public static boolean isIndented(String out, int indentSize) throws Exception {
    BufferedReader reader = new BufferedReader(new StringReader(out));

    boolean indentated = false;

    int level = 0;
    int line = 0;

    String string = reader.readLine();
    while (string != null) {
        int newLevel = 0;
        while (newLevel < string.length()) {
            if (!Character.isWhitespace(string.charAt(newLevel))) {
                break;
            }
            newLevel++;
        }
        if ((newLevel % indentSize) != 0) {
            throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line);
        }
        if (Math.abs(level - newLevel) > indentSize) {
            throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel
                    + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars");
        }
        level = newLevel;

        string = reader.readLine();
        line++;

        if (level > 0) {
            indentated = true;
        }
    }

    if (!indentated) {
        // see if a simple xml piece
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out));

        int elementMaxLevel = -1;
        int elementLevel = 0;
        do {
            int event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                elementLevel++;

                if (elementMaxLevel < elementLevel) {
                    elementMaxLevel = elementLevel;
                }
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                elementLevel--;
            }
        } while (parser.hasNext());

        if (elementMaxLevel > 1) { // should be indentated
            return false;
        }
        return true;
    }

    return indentated;
}