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:de.homelab.madgaksha.lotsofbs.util.LocaleRootWordUtils.java

/**
 * <p>//  www  .  j  av a2s  . com
 * Swaps the case of a String using a word based algorithm.
 * </p>
 * 
 * <ul>
 * <li>Upper case character converts to Lower case</li>
 * <li>Title case character converts to Lower case</li>
 * <li>Lower case character after Whitespace or at start converts to Title
 * case</li>
 * <li>Other Lower case character converts to Upper case</li>
 * </ul>
 * 
 * <p>
 * Whitespace is defined by {@link Character#isWhitespace(char)}. A
 * <code>null</code> input String returns <code>null</code>.
 * </p>
 * 
 * <pre>
 * StringUtils.swapCase(null)                 = null
 * StringUtils.swapCase("")                   = ""
 * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
 * </pre>
 * 
 * @param str
 *            the String to swap case, may be null
 * @return the changed String, <code>null</code> if null String input
 */
public static String swapCase(final String str) {
    if (StringUtils.isEmpty(str)) {
        return str;
    }
    final char[] buffer = str.toCharArray();

    boolean whitespace = true;

    for (int i = 0; i < buffer.length; i++) {
        final char ch = buffer[i];
        if (Character.isUpperCase(ch)) {
            buffer[i] = Character.toLowerCase(ch);
            whitespace = false;
        } else if (Character.isTitleCase(ch)) {
            buffer[i] = Character.toLowerCase(ch);
            whitespace = false;
        } else if (Character.isLowerCase(ch)) {
            if (whitespace) {
                buffer[i] = Character.toTitleCase(ch);
                whitespace = false;
            } else {
                buffer[i] = Character.toUpperCase(ch);
            }
        } else {
            whitespace = Character.isWhitespace(ch);
        }
    }
    return new String(buffer);
}

From source file:eionet.cr.web.action.admin.harvestscripts.HarvestScriptParser.java

/**
 *
 * @param str/*from w w  w .j av a2s.  c  o m*/
 * @return
 */
private static String ensureWhitespaceStart(String str) {

    if (str == null) {
        return null;
    } else if (str.length() == 0 || !Character.isWhitespace(str.charAt(0))) {
        return " " + str;
    } else {
        return str;
    }
}

From source file:de.codesourcery.jasm16.utils.Misc.java

public static String removeLeadingWhitespace(String input) {
    StringBuilder output = new StringBuilder(input);
    while (output.length() > 0 && Character.isWhitespace(output.charAt(0))) {
        output.delete(0, 1);// w  ww .j  a va 2s  . c o m
    }
    return output.toString();
}

From source file:interactivespaces.util.process.BaseNativeApplicationRunner.java

/**
 * Extract command line flags from a string.
 *
 * @param commandLineComponents/*  ww w. j  a va  2s.  c om*/
 *          the list to place the command flags in
 * @param commandFlags
 *          the string containing the flags
 */
private void extractCommandFlags(List<String> commandLineComponents, String commandFlags) {
    if (commandFlags == null) {
        return;
    }

    // Now collect the individual arguments. The escape character will always
    // pass through the following character as part of the current token.
    StringBuilder component = new StringBuilder();
    for (int i = 0; i <= commandFlags.length(); i++) {
        // Force a space on the end to keep the end of a term processing from being duplicated.
        char ch = (i == commandFlags.length()) ? ' ' : commandFlags.charAt(i);
        if (Character.isWhitespace(ch)) {
            if (component.length() != 0) {
                commandLineComponents.add(component.toString());
                component.setLength(0);
            }
        } else if (ch == ESCAPE_CHARACTER) {
            i++;
            if (i < commandFlags.length()) {
                component.append(commandFlags.charAt(i));
            }
        } else {
            component.append(ch);
        }
    }
}

From source file:eionet.cr.web.action.admin.harvestscripts.HarvestScriptParser.java

/**
 *
 * @param str/*  w  ww .  j a  va  2s.  c  om*/
 * @return
 */
private static String ensureWhitespaceEnd(String str) {

    if (str == null) {
        return null;
    } else if (str.length() == 0 || !Character.isWhitespace(str.charAt(str.length() - 1))) {
        return str + " ";
    } else {
        return str;
    }
}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

private StringBuffer getCurrentWord(JTextComponent comp) {
    StringBuffer res = new StringBuffer();
    String upToCaret;/*from  ww  w  .ja va2s .  co m*/

    try {
        upToCaret = comp.getText(0, comp.getCaretPosition());
        // We now have the text from the start of the field up to the caret position.
        // In most fields, we are only interested in the currently edited word, so we
        // seek from the caret backward to the closest space:
        if (!completer.isSingleUnitField()) {
            if ((comp.getCaretPosition() < comp.getText().length())
                    && Character.isWhitespace(comp.getText().charAt(comp.getCaretPosition()))) {
                // caret is in the middle of the text AND current character is a whitespace
                // that means: a new word is started and there is no current word
                return null;
            }

            int piv = upToCaret.length() - 1;
            while ((piv >= 0) && !Character.isWhitespace(upToCaret.charAt(piv))) {
                piv--;
            }
            // priv points to whitespace char or priv is -1
            // copy everything from the next char up to the end of "upToCaret" 
            res.append(upToCaret.substring(piv + 1));
        } else {
            // For fields such as "journal" it is more reasonable to try to complete on the entire
            // text field content, so we skip the searching and keep the entire part up to the caret:
            res.append(upToCaret);
        }
        //Util.pr("AutoCompListener: "+res.toString());
    } catch (BadLocationException ignore) {
    }

    return res;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Strips any of a set of characters from the end of a String.</p>
 *
 * <p>A {@code null} input String returns {@code null}.
 * An empty string ("") input returns the empty string.</p>
 *
 * <p>If the stripChars String is {@code null}, whitespace is
 * stripped as defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>//from  ww  w  .  j  ava  2  s.  co m
 * 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"
 * StringUtils.stripEnd("120.00", ".0")   = "12"
 * </pre>
 *
 * @param str  the String to remove characters from, may be null
 * @param stripChars  the set of characters to remove, null treated as whitespace
 * @return the stripped String, {@code null} 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)) != INDEX_NOT_FOUND)) {
            end--;
        }
    }
    return str.substring(0, end);
}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

private StringBuffer getCurrentWord(JTextComponent comp) {
    StringBuffer res = new StringBuffer();
    String upToCaret;/* w  ww.j ava2 s . c o  m*/

    try {
        upToCaret = comp.getText(0, comp.getCaretPosition());
        // We now have the text from the start of the field up to the caret position.
        // In most fields, we are only interested in the currently edited word, so we
        // seek from the caret backward to the closest space:
        if (!completer.isSingleUnitField()) {
            if ((comp.getCaretPosition() < comp.getText().length())
                    && Character.isWhitespace(comp.getText().charAt(comp.getCaretPosition()))) {
                // caret is in the middle of the text AND current character is a whitespace
                // that means: a new word is started and there is no current word
                return new StringBuffer();
            }

            int piv = upToCaret.length() - 1;
            while ((piv >= 0) && !Character.isWhitespace(upToCaret.charAt(piv))) {
                piv--;
            }
            // piv points to whitespace char or piv is -1
            // copy everything from the next char up to the end of "upToCaret"
            res.append(upToCaret.substring(piv + 1));
        } else {
            // For fields such as "journal" it is more reasonable to try to complete on the entire
            // text field content, so we skip the searching and keep the entire part up to the caret:
            res.append(upToCaret);
        }
        LOGGER.debug("AutoCompListener: " + res);
    } catch (BadLocationException ignore) {
        // Ignored
    }

    return res;
}

From source file:com.screenslicer.core.util.Util.java

public static int trimmedLen(String str) {
    if (str.isEmpty()) {
        return 0;
    }//from   w  w  w. j a v  a  2  s .c om
    int count = 0;
    boolean prevWhitespace = false;
    str = str.replaceAll("&nbsp;", " ").replaceAll("&amp;nbsp;", " ").trim();
    for (int i = 0; i < str.length(); i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            ++count;
            prevWhitespace = false;
        } else if (!prevWhitespace) {
            ++count;
            prevWhitespace = true;
        }
    }
    return count;
}

From source file:eionet.cr.web.action.admin.harvestscripts.HarvestScriptParser.java

/**
 *
 * @param str/*from  w  w w .j a v  a2  s.  co m*/
 * @return
 */
private static String ensureWhitespaceStartEnd(String str) {

    if (str == null) {
        return null;
    } else if (str.length() == 0) {
        return " ";
    } else {
        String result = str;
        if (!Character.isWhitespace(result.charAt(0))) {
            result = " " + result;
        }
        if (!Character.isWhitespace(result.charAt(result.length() - 1))) {
            result = result + " ";
        }
        return result;
    }
}