Example usage for java.lang CharSequence length

List of usage examples for java.lang CharSequence length

Introduction

In this page you can find the example usage for java.lang CharSequence length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:StringUtils.java

/**
 * @param stripChars if null, remove leading unicode whitespaces.
 *//*w w  w.  j  ava  2  s.com*/
public static CharSequence stripEnd(CharSequence src, String stripChars) {
    int end;
    if (src == null || (end = src.length()) == 0) {
        return src;
    }
    if (stripChars == null) {
        while ((end != 0) && Character.isWhitespace(src.charAt(end - 1))) {
            end--;
        }
    } else if (stripChars.length() == 0) {
        return src;
    } else {
        while ((end != 0) && (stripChars.indexOf(src.charAt(end - 1)) != -1)) {
            end--;
        }
    }
    return src.subSequence(0, end);
}

From source file:Main.java

/**
 * Append newText to the text field represented by connection.
 * The new text becomes selected./*w  ww .  j  a v  a  2  s .co  m*/
 */
public static void appendText(InputConnection connection, String newText) {
    if (connection == null) {
        return;
    }

    // Commit the composing text
    connection.finishComposingText();

    // Add a space if the field already has text.
    CharSequence charBeforeCursor = connection.getTextBeforeCursor(1, 0);
    if (charBeforeCursor != null && !charBeforeCursor.equals(" ") && (charBeforeCursor.length() > 0)) {
        newText = " " + newText;
    }

    connection.setComposingText(newText, 1);
}

From source file:Main.java

public static void setEditTextCursorLocation(EditText editText) {
    CharSequence text = editText.getText();
    if (text instanceof Spannable) {
        Spannable spanText = (Spannable) text;
        Selection.setSelection(spanText, text.length());
    }/*ww  w .j a va  2 s .co  m*/
}

From source file:Main.java

/**
 * Green implementation of toCharArray.//from w w  w . java 2 s .c  o m
 *
 * @param cs the {@code CharSequence} to be processed
 * @return the resulting char array
 */
static char[] toCharArray(final CharSequence cs) {
    if (cs instanceof String) {
        return ((String) cs).toCharArray();
    } else {
        final int sz = cs.length();
        final char[] array = new char[cs.length()];
        for (int i = 0; i < sz; i++) {
            array[i] = cs.charAt(i);
        }
        return array;
    }
}

From source file:Strings.java

/**
 * Return a displayable version of the character sequence,
 * followed by integer positions at various powers of 10.
 * For instance, for the input string \code{John ran home.}, the
 * output is/*from  w  w w  .  j  av  a 2s .co m*/
 *
 * <blockquote><pre>
 * John ran home.
 * 01234567890123
 * 0         1
 * </pre></blockquote>
 *
 * This allows easy access to the index of positions in the
 * string.
 *
 * @param in Input sequence to annotate.
 * @return The input sequence followed by indexing positions.
 */
public static String textPositions(CharSequence in) {
    StringBuilder sb = new StringBuilder();
    sb.append(in);
    for (int base = 1; base <= in.length(); base *= 10) {
        sb.append('\n');
        for (int i = 0; i < in.length(); ++i)
            sb.append(i % base == 0 ? Integer.toString((i / base) % 10) : " ");
    }
    return sb.toString();
}

From source file:Main.java

public static void setCursorLocation(Context context, EditText editText) {
    CharSequence text = editText.getText();
    if (text instanceof Spannable) {
        Spannable spanText = (Spannable) text;
        Selection.setSelection(spanText, text.length());
    }// w  w  w  .ja v a  2 s  . c o  m
}

From source file:com.hj.blog.common.utils.StringUtils.java

static boolean isNumeric(final CharSequence cs) {
    if (isEmpty(cs)) {
        return false;
    }/*from  w  w  w.j  av  a2s  .c  o  m*/
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isDigit(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Check whether the given CharSequence has actual text.
 * More specifically, returns {@code true} if the string not {@code null},
 * its length is greater than 0, and it contains at least one non-whitespace character.
 * <p><pre class="code">//from   w w w .  j av a  2 s .c o  m
 * StringUtils.hasText(null) = false
 * StringUtils.hasText("") = false
 * StringUtils.hasText(" ") = false
 * StringUtils.hasText("12345") = true
 * StringUtils.hasText(" 12345 ") = true
 * </pre>
 * @param str the CharSequence to check (may be {@code null})
 * @return {@code true} if the CharSequence is not {@code null},
 * its length is greater than 0, and it does not contain whitespace only
 * @see Character#isWhitespace
 */
public static boolean hasText(CharSequence str) {
    if (!hasLength(str)) {
        return false;
    }
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:StringUtils.java

/**
 * @param stripChars if null, remove leading unicode whitespaces.
 *//*from w  w  w . ja  v  a2  s. 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

/**
 * Applies the given style to a range of the input CharSequence.
 * @param style The style to apply (see the style constants in {@link Typeface}).
 * @param input The CharSequence to style.
 * @param start Starting index of the range to style (will be clamped to be a minimum of 0).
 * @param end Ending index of the range to style (will be clamped to a maximum of the input
 *     length).//from ww w .j  a  v  a  2s  . c  o  m
 * @param flags Bitmask for configuring behavior of the span.  See {@link android.text.Spanned}.
 * @return The styled CharSequence.
 */
public static CharSequence applyStyleToSpan(int style, CharSequence input, int start, int end, int flags) {
    // Enforce bounds of the char sequence.
    start = Math.max(0, start);
    end = Math.min(input.length(), end);
    SpannableString text = new SpannableString(input);
    text.setSpan(new StyleSpan(style), start, end, flags);
    return text;
}