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:com.alibaba.napoli.metamorphosis.utils.DiamondUtils.java

private static String trim(final String str, final String stripChars, final int mode) {
    if (str == null) {
        return null;
    }/*from   w w w  . ja v  a  2  s  .c o  m*/

    final int length = str.length();
    int start = 0;
    int end = length;

    // ??
    if (mode <= 0) {
        if (stripChars == null) {
            while (start < end && Character.isWhitespace(str.charAt(start))) {
                start++;
            }
        } else if (stripChars.length() == 0) {
            return str;
        } else {
            while (start < end && stripChars.indexOf(str.charAt(start)) != -1) {
                start++;
            }
        }
    }

    // ??
    if (mode >= 0) {
        if (stripChars == null) {
            while (start < end && Character.isWhitespace(str.charAt(end - 1))) {
                end--;
            }
        } else if (stripChars.length() == 0) {
            return str;
        } else {
            while (start < end && stripChars.indexOf(str.charAt(end - 1)) != -1) {
                end--;
            }
        }
    }

    if (start > 0 || end < length) {
        return str.substring(start, end);
    }

    return str;
}

From source file:cn.lambdalib.util.client.font.Fragmentor.java

TokenType getType(char ch) {
    if (isPunct(ch)) {
        return TokenType.PUNCT;
    } else if (Character.isWhitespace(ch)) {
        return TokenType.SPACE;
    } else if (Character.isIdeographic(ch)) {
        return TokenType.CJKV;
    } else {// w  w  w. j a va  2  s. c  o  m
        return TokenType.WORD;
    }
}

From source file:com.igormaznitsa.mindmap.plugins.importers.Text2MindMapImporter.java

private int calcDataOffset(@Nonnull final String text) {
    int result = 0;
    for (final char c : text.toCharArray()) {
        if (c == '\t') {
            result += TAB_POSITIONS - (result % TAB_POSITIONS);
        } else if (Character.isWhitespace(c)) {
            result++;/*from w w  w.  j  a va  2  s.  c om*/
        } else {
            break;
        }
    }
    return result;
}

From source file:CSVReader.java

/**
 * categorise a character for the finite state machine.
 *
 * @param c      the character to categorise
 * @return integer representing the character's category.
 *//*w ww. j  a v  a 2  s  .c  om*/
private int categorise(char c) {
    switch (c) {
    case ' ':
    case '\r':
    case 0xff:
        return WHITESPACE;
    //         case ';':
    //         case '!':
    case '#':
        //return EOL;
    case '\n':
        return EOL; /* artificially applied to end of line */
    case '\"':
        return QUOTE;
    default:
        if (c == separator) {
            /* dynamically determined so can't use as case label */
            return SEPARATOR;
        } else if ('!' <= c && c <= '~') {
            /* do our tests in crafted order, hoping for an early return */
            return ORDINARY;
        } else if (0x00 <= c && c <= 0x20) {
            return WHITESPACE;
        } else if (Character.isWhitespace(c)) {
            return WHITESPACE;
        } else {
            return ORDINARY;
        }
    } // end of switch
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlSerializer.java

private String reduceWhitespace(String text) {
    text = text.trim();//  ww w . j  a  va  2 s .  c  o m

    // remove white spaces before or after block separators
    text = reduceWhiteSpaceAroundBlockSeparator(text);

    // remove leading block separators
    while (text.startsWith(AS_TEXT_BLOCK_SEPARATOR)) {
        text = text.substring(AS_TEXT_BLOCK_SEPARATOR_LENGTH);
    }

    // remove trailing block separators
    while (text.endsWith(AS_TEXT_BLOCK_SEPARATOR)) {
        text = text.substring(0, text.length() - AS_TEXT_BLOCK_SEPARATOR_LENGTH);
    }
    text = text.trim();

    final StringBuilder buffer = new StringBuilder(text.length());

    boolean whitespace = false;
    for (final char ch : text.toCharArray()) {

        // Translate non-breaking space to regular space.
        if (ch == (char) 160) {
            buffer.append(' ');
            whitespace = false;
        } else {
            if (whitespace) {
                if (!Character.isWhitespace(ch)) {
                    buffer.append(ch);
                    whitespace = false;
                }
            } else {
                if (Character.isWhitespace(ch)) {
                    whitespace = true;
                    buffer.append(' ');
                } else {
                    buffer.append(ch);
                }
            }
        }
    }
    return buffer.toString();
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlSerializer.java

private static String reduceWhiteSpaceAroundBlockSeparator(final String text) {
    int p0 = text.indexOf(AS_TEXT_BLOCK_SEPARATOR);
    if (p0 == -1) {
        return text;
    }//from   w  ww  .j  a v  a 2 s.  c  o m

    final int length = text.length();
    if (length <= AS_TEXT_BLOCK_SEPARATOR_LENGTH) {
        return text;
    }

    final StringBuilder result = new StringBuilder(length);
    int start = 0;
    while (p0 != -1) {
        int p1 = p0 + AS_TEXT_BLOCK_SEPARATOR_LENGTH;
        while (p0 != start && Character.isWhitespace(text.charAt(p0 - 1))) {
            p0--;
        }
        if (p0 >= AS_TEXT_NEW_LINE_LENGTH && text.startsWith(AS_TEXT_NEW_LINE, p0 - AS_TEXT_NEW_LINE_LENGTH)) {
            p0 = p0 - AS_TEXT_NEW_LINE_LENGTH;
        }
        result.append(text.substring(start, p0)).append(AS_TEXT_BLOCK_SEPARATOR);

        while (p1 < length && Character.isWhitespace(text.charAt(p1))) {
            p1++;
        }
        start = p1;

        // ignore duplicates
        p0 = text.indexOf(AS_TEXT_BLOCK_SEPARATOR, start);
        while (p0 != -1 && p0 == start) {
            start += AS_TEXT_BLOCK_SEPARATOR_LENGTH;
            p0 = text.indexOf(AS_TEXT_BLOCK_SEPARATOR, start);
        }
    }
    if (start < length) {
        result.append(text.substring(start));
    }
    return result.toString();
}

From source file:com.spider.ssh.SshConfigFile.java

/**
 * Splits a property line from the source file into two pieces, a name and a value.
 * This method is space preserving so that any whitespace in the value is kept
 * as it was in the source file.//  w  w w. j a  va2  s.  co  m
 * @param s The property line from the config file
 * @return
 */
protected String[] splitProperty(String s) {
    s = s.trim();
    String[] result = new String[2];
    int location = -1;

    char[] data = s.toCharArray();
    for (int i = 0; i < data.length; i++) {
        if (Character.isWhitespace(data[i])) {
            location = i;
            break;
        }
    }

    if (location > 0) {
        result[0] = s.substring(0, location);
        result[1] = s.substring(location + 1).trim();
        if (StringUtils.isBlank(result[1])) {
            result[1] = null;
        }
    } else {
        result[0] = s;
    }

    return result;
}

From source file:com.google.dart.tools.ui.internal.text.dart.DartAutoIndentStrategy_NEW.java

private static String getWhitespaceRight(IDocument document, int index) throws BadLocationException {
    StringBuffer result = new StringBuffer();
    while (true) {
        char c = document.getChar(index++);
        if (!Character.isWhitespace(c)) {
            break;
        }//from w w w .  ja v  a 2 s.c  o m
        result.append(c);
    }
    return result.toString();
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.smileys.GWikiSmileyContentIterator.java

private int scanForSmiley(int i, String text) {
    int startIdx = i;
    for (; i < text.length(); ++i) {
        char c = text.charAt(i);
        if (c == ')') {
            String smtxt = text.substring(startIdx, i);
            if (smileyConfig.getSmileys().containsKey(smtxt) == true) {
                return i;
            }/*  w ww  .  ja  v a  2  s  . c o m*/
        } else if (Character.isWhitespace(c) == true) {
            return -1;
        }
    }
    return -1;
}

From source file:net.bioclipse.pubchem.business.PubChemManager.java

private String replaceSpaces(String molecule2) {
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < molecule2.length(); i++) {
        if (Character.isWhitespace(molecule2.charAt(i))) {
            result.append("+");
        } else {/*from   w w w  .  jav a  2s .c  o  m*/
            result.append(molecule2.charAt(i));
        }
    }
    return result.toString();
}