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

/**
 * Test if the current character is a space.
 * //  w ww . j a  va  2  s .co m
 * @param charArray The buffer which contains the data
 * @param pos Position in the buffer
 *
 * @return <code>true</code> if the current character is a space.
 */
public static final boolean isSpace(char[] charArray, int index) {
    if ((charArray == null) || (charArray.length == 0) || (index < 0) || (index >= charArray.length)) {
        return false;
    } else {
        return (Character.isWhitespace(charArray[index]) ? true : false);
    }
}

From source file:StringUtils.java

/**
 * @param stripChars if null, remove leading unicode whitespaces.
 *///w  w  w  . ja v a  2s . c o  m
public static CharSequence stripStart(CharSequence src, String stripChars) {
    int srclen;
    if (src == null || (srclen = src.length()) == 0) {
        return src;
    }
    int start = 0;
    if (stripChars == null) {
        while ((start != srclen) && Character.isWhitespace(src.charAt(start))) {
            start++;
        }
    } else if (stripChars.length() == 0) {
        return src;
    } else {
        while ((start != srclen) && (stripChars.indexOf(src.charAt(start)) != -1)) {
            start++;
        }
    }
    return src.subSequence(start, srclen);
}

From source file:Main.java

/**
 * Returns the name of the first XML tag in the argument string.
 * @param inStr The argument String to search.
 * @return The name of the first XML tag in the argument string,
 * or null if the operation fails.//from  w  w  w . java 2  s . c o  m
 */
public static String firstTag(String inStr) {
    int startIndex = inStr.indexOf("<");
    if (startIndex < 0)
        return null;

    // skip comments
    while (inStr.substring(startIndex).equals("<!--")) {
        inStr = inStr.substring(startIndex + 4);
        startIndex = inStr.indexOf("-->");
        if (startIndex < 0)
            return null;

        startIndex = inStr.indexOf("<");
        if (startIndex < 0)
            return null;
    } // while

    int tagOffset = 1;
    while (Character.isWhitespace(inStr.charAt(startIndex + tagOffset))) {
        ++tagOffset;
    } // while

    int tokenLen = 0;
    if (isStartChar(inStr.charAt(startIndex + tagOffset))) {
        ++tokenLen;
        while (isNameChar(inStr.charAt(startIndex + tagOffset + tokenLen))) {
            ++tokenLen;
        } // while
    } // if

    char endChar = inStr.charAt(startIndex + tagOffset + tokenLen);
    if (!(Character.isWhitespace(endChar) || (endChar == '>'))) {
        return null;
    } // if

    return inStr.substring(startIndex + tagOffset, startIndex + tagOffset + tokenLen);
}

From source file:com.boundary.plugin.sdk.PluginUtil.java

/**
 * Transform a camel case string into a upper case string with underscores
 * /*from ww w.  j av  a  2  s.  co m*/
 * @param s {@link String} to transform
 * @param d replacement character for white space
 * @return {@link String}
 */
public static String toUpperUnderscore(String s, Character d) {
    StringBuilder builder = new StringBuilder();
    s = s.trim();

    boolean first = true;
    for (int i = 0; i < s.length(); i++) {
        if (Character.isWhitespace(s.charAt(i))) {
            builder.append(d);
        } else {
            if (Character.isUpperCase(s.charAt(i))) {
                if (first == false) {
                    if (i + 1 < s.length() && Character.isLowerCase(s.charAt(i + 1))) {
                        builder.append(d);
                    }
                }
            }
            builder.append(Character.toUpperCase(s.charAt(i)));
        }
        first = false;
    }
    StringBuilder r = new StringBuilder();
    r.append('\\').append(d).append('\\').append(d);

    return builder.toString().replaceAll(r.toString(), d.toString());
}

From source file:Main.java

/**
 * /*  w  w w .ja  v  a 2s .  com*/
 * @param caracter
 * @param pos
 * @param cadena
 * @return
 */
private static boolean esElCaracterElPrimerPredecesorQueNoSeaBlanco(char caracter, int pos, String cadena) {
    if (pos == 0)
        return false;

    while (true) {
        pos = pos - 1;
        char predecesor = cadena.charAt(pos);
        if ((predecesor == caracter && pos == 0) || (predecesor == caracter && cadena.charAt(pos - 1) != '\\')) //para saltar los caracteres con \
            return true;
        if (!Character.isWhitespace(predecesor) && predecesor != caracter)
            return false;
        if (pos == 0)
            return false;
    }
}

From source file:Main.java

/***
 * Fast xml formatter without loading xml into a document or using a 
 * serializer. //from   w w w.ja  v a2s  .c  o m
 * 
 * @param content
 * @param lineBreaker
 * @param nodeSpacer
 * @return
 */
public static String prettyPrintNoDoc(String content, String lineBreaker, String nodeSpacer) {

    if (content == null || content.length() == 0) {
        return null;
    } else {
        StringBuilder prettyPrintBuilder = new StringBuilder(content.length() + 200);

        boolean inQuote = false;
        boolean inData = false;
        int nodeDepth = 0;
        char prevCh = 0;
        char lastMeaningfulChar = 0;

        for (int i = 0; i < content.length(); i++) {

            char ch = content.charAt(i);

            char nextCh = i < content.length() - 1 ? content.charAt(i + 1) : 0;

            if (inData) { // We are in <!-- or <!CDATA[[ block, find when to
                          // exit it
                if ('>' == ch && prettyPrintBuilder.length() >= 2) {
                    String lastTwoChar = prettyPrintBuilder.substring(prettyPrintBuilder.length() - 2);
                    if ("--".equals(lastTwoChar) || "]]".equals(lastTwoChar)) {
                        inData = false;

                        lastMeaningfulChar = '>';
                    }
                }

                prettyPrintBuilder.append(ch);

            } else if (inQuote) { // in the quote, fine when to exit it
                if ('"' == ch) {
                    inQuote = false;
                }

                prettyPrintBuilder.append(ch);

            } else { // Not in quote or data
                if (Character.isWhitespace(ch)) {
                    if (!Character.isISOControl(prevCh) && !Character.isWhitespace(prevCh) && '>' != prevCh) {
                        prettyPrintBuilder.append(' ');
                    }
                    ch = 0;
                } else if ('"' == ch) {
                    inQuote = true;
                } else if ('<' == ch) { // We are at the start of a node

                    if ('?' == nextCh) { // Start declaration node
                        // move the nodeDepth to -1;
                        nodeDepth = -1;
                    } else if ('!' == nextCh) { // Start Data node
                        inData = true;
                        prettyPrintBuilder.append(lineBreaker);
                        appendSpace(prettyPrintBuilder, nodeSpacer, nodeDepth + 1);
                    } else if (i > 1) { // We are at the start of a regular
                        // node or closing a regular node

                        if ('>' == lastMeaningfulChar) { // A node just
                            // finished
                            // above
                            prettyPrintBuilder.append(lineBreaker);

                            if ('/' == nextCh) {// At closing of an node
                                // section </
                                appendSpace(prettyPrintBuilder, nodeSpacer, nodeDepth);
                                nodeDepth--;
                            } else { // At starting of a node
                                nodeDepth++;
                                appendSpace(prettyPrintBuilder, nodeSpacer, nodeDepth);
                            }
                        } else if ('/' == nextCh) { // At closing of an node
                            nodeDepth--;
                        }

                    }
                } else if ('/' == ch && '>' == nextCh) { // closing a node with />
                    nodeDepth--;
                }

                if (!Character.isISOControl(ch) && ch != 0) {
                    prettyPrintBuilder.append(ch);

                    if (!Character.isWhitespace(ch)) {
                        lastMeaningfulChar = ch;
                    }
                }
            }

            prevCh = ch;
        }
        return prettyPrintBuilder.toString();
    }

}

From source file:Main.java

public static String stripEnd(String str, String stripChars) {
    int end;//from   w w  w. j  av a  2s  . co  m
    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:edu.stanford.muse.util.SloppyDates.java

private static Triple<Integer, Integer, Integer> parseDate(String s) {
    // separate into month and date
    // "jan10", "10jan", "jan 10" "10 jan" should all work
    s = s.toLowerCase();// w  w w  .j  ava2  s.c  o m
    s = s.trim();
    StringBuilder sb = new StringBuilder();
    // detect when string changes from alpha to num or vice versa and ensure a whitespace there
    boolean prevCharDigit = false, prevCharLetter = false;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (Character.isWhitespace(c)) {
            sb.append(c);
            prevCharDigit = prevCharLetter = false;
            continue;
        }
        // treat apostrophe like a space
        if (c == '\'') {
            sb.append(' ');
            prevCharDigit = prevCharLetter = false;
            continue;
        }

        if (Character.isLetter(c)) {
            if (prevCharDigit)
                sb.append(' ');
            sb.append(c);
            prevCharLetter = true;
            prevCharDigit = false;
        } else if (Character.isDigit(c)) {
            if (prevCharLetter)
                sb.append(' ');
            sb.append(c);
            prevCharDigit = true;
            prevCharLetter = false;
        } else
            throw new RuntimeException();
    }

    String newS = sb.toString();
    log.info("string " + s + " parsed to " + newS);
    StringTokenizer st = new StringTokenizer(newS);

    int nTokens = st.countTokens();
    if (nTokens == 0 || nTokens > 3)
        return new Triple<Integer, Integer, Integer>(-1, -1, -1);

    int mm = -1, dd = -1, yy = -1;
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        boolean isNumber = true;
        int num = -1;
        try {
            num = Integer.parseInt(token);
        } catch (NumberFormatException nfe) {
            isNumber = false;
        }
        if (isNumber && num < 0)
            return new Triple<Integer, Integer, Integer>(-1, -1, -1);
        if (isNumber) {
            if (dd == -1 && num > 0 && num <= 31)
                dd = num;
            else if (yy == -1) {
                yy = num;
                if (yy < 100) {
                    yy = (yy > 12) ? (1900 + yy) : (2000 + yy);
                }
                if (yy < 1900 || yy > 2015)
                    return new Triple<Integer, Integer, Integer>(-1, -1, -1);
            } else
                return new Triple<Integer, Integer, Integer>(-1, -1, -1);
        } else {
            int x = SloppyDates.uniquePrefixIdx(token, monthNames);
            if (x >= 0 && mm == -1)
                mm = x;
            else
                return new Triple<Integer, Integer, Integer>(-1, -1, -1);
        }
    }
    return new Triple<Integer, Integer, Integer>(dd, mm, yy);
}

From source file:com.norbl.util.StringUtil.java

public static boolean containsWhiteSpace(String s) {
    for (int i = 0; i < s.length(); i++) {
        if (Character.isWhitespace(s.charAt(i)))
            return (true);
    }/*from w ww  .j  ava2 s  .co m*/
    return (false);
}

From source file:gov.nih.nci.cabig.ctms.lang.StringTools.java

/**
 * Normalizes the whitespace in the given buffer in-place.
 * This means that the whitespace is stripped from the head and the tail
 * and any contiguous stretches of whitespace are converted into a single
 * space./*from   w  w  w.j  a  v  a  2s. c o m*/
 *
 * @param toNormalize
 * @return the passed-in buffer
 */
public static StringBuffer normalizeWhitespace(StringBuffer toNormalize) {
    if (toNormalize == null)
        return null;
    // start with this value == true to completely remove leading whitespace
    boolean prevIsWhitespace = true;
    for (int i = 0; i < toNormalize.length(); i++) {
        if (Character.isWhitespace(toNormalize.charAt(i))) {
            if (prevIsWhitespace) {
                toNormalize.deleteCharAt(i);
                i--;
            } else {
                toNormalize.setCharAt(i, ' ');
                prevIsWhitespace = true;
            }
        } else {
            prevIsWhitespace = false;
        }
    }

    // remove (at most) one trailing ' '
    if (toNormalize.length() > 0 && toNormalize.charAt(toNormalize.length() - 1) == ' ')
        toNormalize.deleteCharAt(toNormalize.length() - 1);
    return toNormalize;
}