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

public static int computeLevenshteinDistance(CharSequence str1, CharSequence str2) {
    int[][] distance = new int[str1.length() + 1][str2.length() + 1];
    for (int i = 0; i <= str1.length(); i++) {
        distance[i][0] = i;//from  ww w .j a v  a 2  s .c o  m
    }
    for (int j = 0; j <= str2.length(); j++) {
        distance[0][j] = j;
    }
    for (int i = 1; i <= str1.length(); i++) {
        for (int j = 1; j <= str2.length(); j++) {
            distance[i][j] = minimum(distance[i - 1][j] + 1, distance[i][j - 1] + 1,
                    distance[i - 1][j - 1] + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1));
        }
    }
    return distance[str1.length()][str2.length()];
}

From source file:Main.java

public static String guessAppropriateEncoding(CharSequence contents) {
    // Very crude at the moment
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }/*from www.ja  v a2 s.  c o m*/
    }
    return null;
}

From source file:Main.java

/**
 * is null or its length is 0/*  w ww  . j a v  a2 s . c om*/
 * <p/>
 * <pre>
 * isEmpty(null) = true;
 * isEmpty(&quot;&quot;) = true;
 * isEmpty(&quot;  &quot;) = false;
 * </pre>
 *
 * @param str str
 * @return if string is null or its size is 0, return true, else return
 * false.
 */
public static boolean isEmpty(CharSequence str) {

    return (str == null || str.length() == 0);
}

From source file:Main.java

/**
 * <p>Returns a new {@code CharSequence} that is a subsequence of this
 * sequence starting with the {@code char} value at the specified index.</p>
 *
 * <p>This provides the {@code CharSequence} equivalent to {@link String#substring(int)}.
 * The length (in {@code char}) of the returned sequence is {@code length() - start},
 * so if {@code start == end} then an empty sequence is returned.</p>
 *
 * @param cs  the specified subsequence, null returns null
 * @param start  the start index, inclusive, valid
 * @return a new subsequence, may be null
 * @throws IndexOutOfBoundsException if {@code start} is negative or if 
 *  {@code start} is greater than {@code length()}
 *///from w w  w .  ja  v  a  2  s  .  c  o m
public static CharSequence subSequence(CharSequence cs, int start) {
    return cs == null ? null : cs.subSequence(start, cs.length());
}

From source file:Main.java

/**
 * Check that the given CharSequence is neither {@code null} nor of length 0.
 * Note: Will return {@code true} for a CharSequence that purely consists of whitespace.
 * <p><pre class="code">/*w  w w  .  j a  v a  2s . c  o  m*/
 * StringUtils.hasLength(null) = false
 * StringUtils.hasLength("") = false
 * StringUtils.hasLength(" ") = true
 * StringUtils.hasLength("Hello") = true
 * </pre>
 * @param str the CharSequence to check (may be {@code null})
 * @return {@code true} if the CharSequence is not null and has length
 * @see #hasText(String)
 */
public static boolean hasLength(CharSequence str) {
    return (str != null && str.length() > 0);
}

From source file:Main.java

/**
 * get length of CharSequence//from   ww  w.  j ava  2  s .  c  o  m
 * <p/>
 * <pre>
 * length(null) = 0;
 * length(\"\") = 0;
 * length(\"abc\") = 3;
 * </pre>
 *
 * @param str str
 * @return if str is null or empty, return 0, else return {@link
 * CharSequence#length()}.
 */
public static int length(CharSequence str) {

    return str == null ? 0 : str.length();
}

From source file:Main.java

public static String sanitizeToLineFeed(CharSequence string) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0, len = string.length(); i < len; i++) {
        char c = string.charAt(i);
        boolean legal = c == '\u0009' || c == '\n' || (c >= '\u0020' && c <= '\uD7FF')
                || (c >= '\uE000' && c <= '\uFFFD');
        if (legal) {
            sb.append(c);//from w w  w.j av a2 s  .  c  o m
        }
    }
    return sb.toString();
}

From source file:Main.java

static boolean prefixes(CharSequence sequence, String prefix) {
    boolean prefixes = false;
    if (sequence.length() > prefix.length()) {
        int count = prefix.length();
        prefixes = true;/*  ww w  .j ava  2s  .  c  o m*/
        for (int i = 0; i < count; i++) {
            if (sequence.charAt(i) != prefix.charAt(i)) {
                prefixes = false;
                break;
            }
        }
    }
    return prefixes;
}

From source file:Main.java

public static String sanitizeXml11(CharSequence sequence) {
    if (sequence == null) {
        return null;
    }/*from www  .  j  a v  a2s. c o  m*/
    if (sequence.length() == 0) {
        return "";
    }
    return invalidXml11.matcher(sequence).replaceAll("\uFFFD");
}

From source file:Main.java

/**
 * Clean strings from illegal XML 1.0 characters.
 * See <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets">XML charset</a>
 *
 * @param string string to clean/*  ww w  . j  a  v  a 2s. c  om*/
 * @return the cleaned string
 */
public static String sanitize(CharSequence string) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0, len = string.length(); i < len; i++) {
        char c = string.charAt(i);
        boolean legal = c == '\u0009' || c == '\n' || c == '\r' || (c >= '\u0020' && c <= '\uD7FF')
                || (c >= '\uE000' && c <= '\uFFFD');
        if (legal) {
            sb.append(c);
        }
    }
    return sb.toString();
}