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:org.apache.solr.client.solrj.util.ClientUtils.java

/**
 * See: <a href="http://lucene.apache.org/java/docs/nightly/queryparsersyntax.html#Escaping%20Special%20Characters">Escaping Special Characters</a>
 *///from w  w  w  .j  a  va  2 s . c o m
public static String escapeQueryChars(String s) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        // These characters are part of the query syntax and must be escaped
        if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^'
                || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
                || c == '|' || c == '&' || c == ';' || Character.isWhitespace(c)) {
            sb.append('\\');
        }
        sb.append(c);
    }
    return sb.toString();
}

From source file:StringUtilities.java

public static int getTokenBeginIndex(String selectSQL, String token) {
    String lowerSel = selectSQL.toLowerCase();
    String lowerToken = token.toLowerCase().trim();

    int curPos = 0;
    int count = 0;
    while (-1 != curPos) {
        curPos = lowerSel.indexOf(lowerToken, curPos + lowerToken.length());

        if (-1 < curPos && (0 == curPos || Character.isWhitespace(lowerSel.charAt(curPos - 1)))
                && (lowerSel.length() == curPos + lowerToken.length()
                        || Character.isWhitespace(lowerSel.charAt(curPos + lowerToken.length())))) {
            return curPos;
        }//from  w  w  w . jav a2 s.c  o m
        // If we've loop through one time for each character in the string, 
        // then something must be wrong.  Get out!
        if (count++ > selectSQL.length()) {
            break;
        }
    }

    return curPos;
}

From source file:com.runstate.util.StringW.java

/**
 * Word-wrap a string./*from   w w w.  j a  va  2  s  .  com*/
 *
 * @param str   String to word-wrap
 * @param width int to wrap at
 * @param delim String to use to separate lines
 * @param split String to use to split a word greater than width long
 *
 * @return String that has been word wrapped
 */
static public String wordWrap(String str, int width, String delim, String split) {
    int sz = str.length();

    /// shift width up one. mainly as it makes the logic easier
    width++;

    // our best guess as to an initial size
    StringBuffer buffer = new StringBuffer(sz / width * delim.length() + sz);

    // every line will include a delim on the end
    width = width - delim.length();

    int idx = -1;
    String substr = null;

    // beware: i is rolled-back inside the loop
    for (int i = 0; i < sz; i += width) {

        // on the last line
        if (i > sz - width) {
            buffer.append(str.substring(i));
            //                System.err.print("LAST-LINE: "+str.substring(i));
            break;
        }

        //            System.err.println("loop[i] is: "+i);
        // the current line
        substr = str.substring(i, i + width);

        // is the delim already on the line
        idx = substr.indexOf(delim);
        if (idx != -1) {
            buffer.append(substr.substring(0, idx));
            //                System.err.println("Substr: '"substr.substring(0,idx)+"'");
            buffer.append(delim);
            i -= width - idx - delim.length();

            //                System.err.println("loop[i] is now: "+i);
            //                System.err.println("ounfd-whitespace: '"+substr.charAt(idx+1)+"'.");
            // Erase a space after a delim. Is this too obscure?
            if (substr.length() > idx + 1) {
                if (substr.charAt(idx + 1) != '\n') {
                    if (Character.isWhitespace(substr.charAt(idx + 1))) {
                        i++;
                    }
                }
            }
            //                System.err.println("i -= "+width+"-"+idx);
            continue;
        }

        idx = -1;

        // figure out where the last space is
        char[] chrs = substr.toCharArray();
        for (int j = width; j > 0; j--) {
            if (Character.isWhitespace(chrs[j - 1])) {
                idx = j;
                //                    System.err.println("Found whitespace: "+idx);
                break;
            }
        }

        // idx is the last whitespace on the line.
        //            System.err.println("idx is "+idx);
        if (idx == -1) {
            for (int j = width; j > 0; j--) {
                if (chrs[j - 1] == '-') {
                    idx = j;
                    //                        System.err.println("Found Dash: "+idx);
                    break;
                }
            }
            if (idx == -1) {
                buffer.append(substr);
                buffer.append(delim);
                //                    System.err.print(substr);
                //                    System.err.print(delim);
            } else {
                if (idx != width) {
                    idx++;
                }
                buffer.append(substr.substring(0, idx));
                buffer.append(delim);
                //                    System.err.print(substr.substring(0,idx));
                //                    System.err.print(delim);
                i -= width - idx;
            }
        } else {
            /*
            if(force) {
            if(idx == width-1) {
                buffer.append(substr);
                buffer.append(delim);
            } else {
                // stick a split in.
                int splitsz = split.length();
                buffer.append(substr.substring(0,width-splitsz));
                buffer.append(split);
                buffer.append(delim);
                i -= splitsz;
            }
            } else {
            */
            // insert spaces
            buffer.append(substr.substring(0, idx));
            buffer.append(StringUtils.repeat(" ", width - idx));
            //                    System.err.print(substr.substring(0,idx));
            //                    System.err.print(StringUtils.repeat(" ",width-idx));
            buffer.append(delim);
            //                    System.err.print(delim);
            //                    System.err.println("i -= "+width+"-"+idx);
            i -= width - idx;
            //                }
        }
    }
    //        System.err.println("\n*************");
    return buffer.toString();
}

From source file:com.keevosh.springframework.boot.netbeans.SpringBootConfigurationCompletionProvider.java

static int indexOfWhite(char[] line) {
    int i = line.length;
    while (--i > -1) {
        final char c = line[i];
        if (Character.isWhitespace(c)) {
            return i;
        }/*from www.  ja v a2s. c o  m*/
    }
    return -1;
}

From source file:com.atlassian.extras.decoder.v2.Version2LicenseDecoder.java

private static String removeWhiteSpaces(String licenseData) {
    if ((licenseData == null) || (licenseData.length() == 0)) {
        return licenseData;
    }//ww w .  j  a va 2 s .com
    char[] chars = licenseData.toCharArray();
    StringBuffer buf = new StringBuffer(chars.length);
    for (int i = 0; i < chars.length; i++) {
        if (!Character.isWhitespace(chars[i])) {
            buf.append(chars[i]);
        }
    }
    return buf.toString();
}

From source file:Main.java

/**
 * <p>Strips any of a set of characters from the start 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>/*from   w  w  w.ja v  a2  s .  c  o  m*/
 * StringUtils.stripStart(null, *)          = null
 * StringUtils.stripStart("", *)            = ""
 * StringUtils.stripStart("abc", "")        = "abc"
 * StringUtils.stripStart("abc", null)      = "abc"
 * StringUtils.stripStart("  abc", null)    = "abc"
 * StringUtils.stripStart("abc  ", null)    = "abc  "
 * StringUtils.stripStart(" abc ", null)    = "abc "
 * StringUtils.stripStart("yxabc  ", "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 stripStart(String str, String stripChars) {
    int strLen;
    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:com.playtech.portal.platform.common.util.Validator.java

/**
 * Returns <code>true</code> if the string is an alphanumeric name, meaning
 * it contains nothing but English letters, numbers, and spaces.
 *
 * @param  name the string to check/*from  w w w. j a v  a  2s  . com*/
 * @return <code>true</code> if the string is an Alphanumeric name;
 *         <code>false</code> otherwise
 */
public static boolean isAlphanumericName(String name) {
    if (isNull(name)) {
        return false;
    }

    for (char c : name.trim().toCharArray()) {
        if (!isChar(c) && !isDigit(c) && !Character.isWhitespace(c)) {
            return false;
        }
    }

    return true;
}

From source file:com.github.xbn.text.StringUtil.java

/**
   <p>Eliminate whitespace from the right side of a str_obj.</p>
        //from w w  w  .  j a va 2s  . com
 * @param  str_toPad  May not be {@code null}.
 * @see  #ltrim(Object) ltrim(o)
 */
public static String rtrim(Object str_toPad) {
    String s = null;
    try {
        s = str_toPad.toString();
    } catch (RuntimeException rx) {
        throw CrashIfObject.nullOrReturnCause(str_toPad, "str_toPad", null, rx);
    }
    int i = s.length() - 1;
    while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
        i--;
    }
    return s.substring(0, (i + 1));
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading whitespace from the given String.
 *
 * @param str the String to check// w  w  w  .  j  a  v a  2s . c  om
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:de.jcup.egradle.sdk.builder.action.javadoc.ReplaceJavaDocPartsAction.java

License:asdf

/**
 * Replace javadoc identifier tag and trailing parts: "@param name xyz" will
 * be transformed by "${replacement} xyz"
 * /*  www . ja  v  a 2s . c  o m*/
 * @param text
 * @param javadocId
 * @param replacer
 * @param fetchFullLineAsContent
 *            when <code>true</code> the full line is used as content on
 *            replacement:<br>
 *            <br>
 * 
 *            <pre>
 * "@myTag bla xyz\n" will be replaced by "$replacedContentFor(bla) xyz\n"
 *            </pre>
 * 
 *            When <code>false</code>
 * 
 *            <pre>
 * "@myTag bla xyz\n" will be replaced by "$replacedContentFor(bla xyz)\n"
 *            </pre>
 * 
 * @return replaced string
 */
String replaceJavaDocTagAndTrailingParts(String text, String javadocId, ContentReplacer replacer,
        boolean fetchFullLineAsContent) {
    String result = text;
    while (true) {
        int index = result.indexOf(javadocId);
        if (index == -1) {
            return result;
        }

        String before = StringUtils.substring(result, 0, index);

        int length = result.length();
        StringBuilder content = new StringBuilder();
        boolean leadingWhiteSpaces = true;
        int pos = index + javadocId.length();

        while (pos < length) {
            char c = result.charAt(pos++);
            if (Character.isWhitespace(c)) {
                if (leadingWhiteSpaces) {
                    continue;
                }
                if (fetchFullLineAsContent) {
                    /* we only break when line ends */
                    if (c == '\n') {
                        pos--; // go back one step because after needs this
                               // maybe
                        break;
                    } else {
                        /*
                         * do nothing, simply add the whitespace itself too
                         */
                    }

                } else {
                    /*
                     * default way: every whitespace after leading ones will
                     * break
                     */
                    pos--;
                    break;
                }
            }
            leadingWhiteSpaces = false;
            content.append(c);
        }
        String after = StringUtils.substring(result, pos);
        String replaced = replacer.replace(content.toString());

        result = before + replaced + after;
    }
}