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:Main.java

/**
 * Get the longest NCName that is a suffix of a character sequence.
 * /*w  ww. ja v a 2s  . co  m*/
 * @param s
 *        The character sequence.
 * @return The String which is the longest suffix of the character sequence
 *         {@code s} that is an NCName, or {@code null} if the character
 *         sequence {@code s} does not have a suffix that is an NCName.
 */
@Nullable
public static String getNCNameSuffix(CharSequence s) {
    if (s.length() > 1 && s.charAt(0) == '_' && s.charAt(1) == ':') {
        return null;
    }
    int localPartStartIndex = getNCNameSuffixIndex(s);
    if (localPartStartIndex > -1) {
        return s.toString().substring(localPartStartIndex);
    } else {
        return null;
    }
}

From source file:com.zen.androidhtmleditor.util.TextUtil.java

/**
 * <p>Counts how many times the substring appears in the larger String.</p>
 *
 * <p>A <code>null</code> or empty ("") String input returns <code>0</code>.</p>
 *
 * <pre>/*from  www.j ava  2  s.  c o  m*/
 * StringUtils.countMatches(null, *)       = 0
 * StringUtils.countMatches("", *)         = 0
 * StringUtils.countMatches("abba", null)  = 0
 * StringUtils.countMatches("abba", "")    = 0
 * StringUtils.countMatches("abba", "a")   = 2
 * StringUtils.countMatches("abba", "ab")  = 1
 * StringUtils.countMatches("abba", "xxx") = 0
 * </pre>
 *
 * @param str  the String to check, may be null
 * @param sub  the substring to count, may be null
 * @return the number of occurrences, 0 if either String is <code>null</code>
 */
public static int countMatches(CharSequence str, char sub, int start, int end) {
    if (str.length() == 0) {
        return 0;
    }
    int count = 0;
    for (int index = start; index <= end; index++) {
        if (str.charAt(index) == sub) {
            count++;
        }
    }
    return count;
}

From source file:Main.java

/**
 * Check whether the given CharSequence contains any whitespace characters.
 *
 * @param seq the CharSequence to check (may be {@code null})
 * @return {@code true} if the CharSequence is not empty and
 * contains at least 1 whitespace character
 * @see java.lang.Character#isWhitespace
 * @since 3.0//from  w w  w . j a v  a  2  s .  c  o  m
 */
// From org.springframework.util.StringUtils, under Apache License 2.0
public static boolean containsWhitespace(final CharSequence seq) {
    if (isEmpty(seq)) {
        return false;
    }
    final int strLen = seq.length();
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(seq.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:Strings.java

/**
 * Return <code>true</code> if the two character sequences have
 * the same length and the same characters.  Recall that equality
 * is not refined in the specification of {@link CharSequence}, but
 * rather inherited from {@link Object#equals(Object)}.
 *
 * The related method {@link #hashCode(CharSequence)} returns
 * hash codes consistent with this notion of equality.
 *
 * @param cs1 First character sequence.//  w  w  w .j a  v a  2 s  .c om
 * @param cs2 Second character sequence.
 * @return <code>true</code> if the character sequences yield
 * the same strings.
 */
public static boolean equalCharSequence(CharSequence cs1, CharSequence cs2) {
    if (cs1 == cs2)
        return true;
    int len = cs1.length();
    if (len != cs2.length())
        return false;
    for (int i = 0; i < len; ++i)
        if (cs1.charAt(i) != cs2.charAt(i))
            return false;
    return true;
}

From source file:Main.java

/**
 * Determines if a character sequence is {@code null} or empty.
 * //  ww w.  ja va  2s .co m
 * @param s
 *        The character sequence.
 * @return {@code true} if the character sequence is {@code null},
 *         {@code true} if the character sequence is empty, otherwise
 *         {@code false}.
 */
public static boolean isNullOrEmpty(@Nullable CharSequence s) {
    return s == null || s.length() == 0;
}

From source file:StandardUtilities.java

/**
 * Returns the width of the leading white space in the specified
 * string./*from  w w  w  . ja v  a2s  .  com*/
 * @param str The string
 * @param tabSize The tab size
 * @since jEdit 4.3pre15
 */
public static int getLeadingWhiteSpaceWidth(CharSequence str, int tabSize) {
    int whitespace = 0;
    loop: for (int i = 0; i < str.length(); i++) {
        switch (str.charAt(i)) {
        case ' ':
            whitespace++;
            break;
        case '\t':
            whitespace += tabSize - whitespace % tabSize;
            break;
        default:
            break loop;
        }
    }
    return whitespace;
}

From source file:Main.java

/**
 * Gets the index of the longest NCName that is the suffix of a character
 * sequence./* www. j av  a  2s . c om*/
 * 
 * @param cs
 *           The character sequence.
 * @return Returns the index of the longest suffix of the specified character
 *         sequence <code>cs</code> that is an NCName, or -1 if the character
 *         sequence <code>cs</code> does not have a suffix that is an NCName.
 */
public static int getNCNameSuffixIndex(CharSequence cs) {
    int index = -1;
    for (int i = cs.length() - 1; i > -1; i--) {
        if (!Character.isLowSurrogate(cs.charAt(i))) {
            int c = Character.codePointAt(cs, i);
            if (isNCNameStartChar(c)) {
                index = i;
            }
            if (!isNCNameChar(c)) {
                break;
            }
        }
    }
    return index;
}

From source file:Main.java

public static CharSequence getJellyBeanFix(CharSequence input) {
    ArrayList<Integer> resultList = new ArrayList<Integer>();

    for (int i = 0; i < input.length(); i++) {
        int iValue = (int) input.charAt(i);
        if (iValue == 0x1031) {
            resultList.add(JBFIX_CHAR);//ww  w  .j  ava  2s  . c om
            resultList.add(iValue);
        } else if (iValue == 0x1039) {
            resultList.add(iValue);
            resultList.add(JBFIX_CHAR);
        } else {
            resultList.add(iValue);
        }
    }

    char[] chArray = new char[resultList.size()];
    int count = 0;
    for (Integer ch : resultList) {
        chArray[count++] = (char) ch.intValue();
    }

    return String.valueOf(chArray);
}

From source file:Main.java

public static Spannable applyKerning(CharSequence src, float kerning, int start, int end) {
    if (src == null)
        return null;
    final int srcLength = src.length();
    if (srcLength < 2)
        return src instanceof Spannable ? (Spannable) src : new SpannableString(src);
    if (start < 0)
        start = 0;//from w  w  w .j a  v a2  s.com
    if (end > srcLength)
        end = srcLength;

    final String nonBreakingSpace = "\u00A0";
    final SpannableStringBuilder builder = src instanceof SpannableStringBuilder ? (SpannableStringBuilder) src
            : new SpannableStringBuilder(src);
    for (int i = src.length(); i >= 1; i--) {
        builder.insert(i, nonBreakingSpace);
        builder.setSpan(new ScaleXSpan(kerning), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return builder;
}

From source file:Main.java

public static boolean equals(CharSequence a, CharSequence b) {
    if (a == b)/*w w w .  j a va  2s.  co m*/
        return true;
    int length;
    if (a != null && b != null && (length = a.length()) == b.length()) {
        if (a instanceof String && b instanceof String) {
            return a.equals(b);
        } else {
            for (int i = 0; i < length; i++) {
                if (a.charAt(i) != b.charAt(i))
                    return false;
            }
            return true;
        }
    }
    return false;
}