Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

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

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:Main.java

/**
 * Green implementation of toCharArray.//from  w ww .  j  av  a2s . co  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();
    }
    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:com.xebia.xsdnl.innorater.Crap.java

/**
 * Trim a CharSequence, both ends.//from  www.  ja v  a  2 s .co m
 * @param cs a char sequence.
 * @return a trimmed sub-sequence, or the entire original thing.
 */
public static CharSequence trim(@Nullable CharSequence cs) {
    if (cs == null || cs.length() == 0) {
        return cs;
    }
    int start = 0;
    int end = cs.length();
    for (; end > 1; end--) {
        if (!Character.isWhitespace(cs.charAt(end - 1))) {
            break;
        }
    }
    for (; start < end; start++) {
        if (!Character.isWhitespace(cs.charAt(start))) {
            break;
        }
    }
    return cs.subSequence(start, end);
}

From source file:Main.java

/**
 * Determine if two character sequences contain the same characters.
 *
 * @param cs1/*from  w ww .ja va2s.co m*/
 *     first character sequence
 * @param cs2
 *     second character sequence
 * @return true if both sequences have same length and same character sequence
 */
public static boolean isSameSequence(CharSequence cs1, CharSequence cs2) {
    assert cs1 != null;
    assert cs2 != null;
    if (cs1.length() != cs2.length()) {
        return false;
    } else {
        for (int i = 0, n = cs1.length(); i < n; i++) {
            if (cs1.charAt(i) != cs2.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}

From source file:com.oasisfeng.nevo.decorators.whatsapp.WhatsAppDecorator.java

private static CharSequence trim(final CharSequence cs) {
    final int last = cs.length() - 1;
    int start = 0;
    int end = last;
    while ((start <= end) && (cs.charAt(start) <= ' '))
        start++;//  w  w w  .  j  av  a  2 s .  c om
    while ((end >= start) && (cs.charAt(end) <= ' '))
        end--;
    if (start == 0 && end == last)
        return cs;
    return cs.subSequence(start, end + 1);
}

From source file:Main.java

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

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 w w w. j a  v  a2s  .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:TextUtils.java

/**
 * See {@link String#compareToIgnoreCase(String)}
 * /*from   w  w w  .  j a  va 2s.c  om*/
 * @param s
 * @param t
 * @return See {@link String#compareToIgnoreCase(String)}
 */
public static int compareToIgnoreCase(CharSequence s, CharSequence t) {
    int i = 0;

    while (i < s.length() && i < t.length()) {
        char a = Character.toLowerCase(s.charAt(i));
        char b = Character.toLowerCase(t.charAt(i));

        int diff = a - b;

        if (diff != 0) {
            return diff;
        }

        i++;
    }

    return s.length() - t.length();
}

From source file:Main.java

public static String escapeLiteralValueForSql(final CharSequence s) {

    final int len = s.length();
    final StringBuilder out = new StringBuilder(len * 2);

    for (int i = 0; i < len; i++) {
        final char c = s.charAt(i);
        if ((int) c == (int) '\'') {
            out.append("\\\'");
        } else if ((int) c == (int) '\\') {
            out.append("\\\\\\\\");
        } else if ((int) c == (int) '"') {
            out.append("\\\\\"");
        } else if ((int) c == (int) '\n') {
            out.append("\\\\n");
        } else if ((int) c == (int) '\r') {
            out.append("\\\\r");
        } else if ((int) c == (int) '\t') {
            out.append("\\\\t");
        } else if (isLowUnicode((int) c)) {
            out.append("\\\\u");
            out.append(hexString((int) c, SHORT_ESCAPE_LENGTH - 1));
        } else if (isHighUnicode((int) c)) {
            out.append("\\\\U");
            out.append(hexString((int) c, LONG_ESCAPE_LENGTH - 2));
        } else {/*from w w  w  .ja va 2s  .  co  m*/
            out.append(c);
        }
    }

    return out.toString();
}

From source file:org.apache.kylin.common.util.StringUtil.java

public static int utf8Length(CharSequence sequence) {
    int count = 0;
    for (int i = 0, len = sequence.length(); i < len; i++) {
        char ch = sequence.charAt(i);
        if (ch <= 0x7F) {
            count++;/*  w ww.  j  a v  a 2 s .  c  om*/
        } else if (ch <= 0x7FF) {
            count += 2;
        } else if (Character.isHighSurrogate(ch)) {
            count += 4;
            ++i;
        } else {
            count += 3;
        }
    }
    return count;
}

From source file:com.keylesspalace.tusky.util.LinkHelper.java

/**
 * Finds links, mentions, and hashtags in a piece of text and makes them clickable, associating
 * them with callbacks to notify when they're clicked.
 *
 * @param view the returned text will be put in
 * @param content containing text with mentions, links, or hashtags
 * @param mentions any '@' mentions which are known to be in the content
 * @param listener to notify about particular spans that are clicked
 *///  www  .j a  v a 2s.  c o  m
public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions,
        final LinkListener listener) {

    SpannableStringBuilder builder = new SpannableStringBuilder(content);
    URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
    for (URLSpan span : urlSpans) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        int flags = builder.getSpanFlags(span);
        CharSequence text = builder.subSequence(start, end);
        if (text.charAt(0) == '#') {
            final String tag = text.subSequence(1, text.length()).toString();
            ClickableSpan newSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    listener.onViewTag(tag);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(false);
                }
            };
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        } else if (text.charAt(0) == '@' && mentions != null && mentions.length > 0) {
            String accountUsername = text.subSequence(1, text.length()).toString();
            /* There may be multiple matches for users on different instances with the same
             * username. If a match has the same domain we know it's for sure the same, but if
             * that can't be found then just go with whichever one matched last. */
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.localUsername.equalsIgnoreCase(accountUsername)) {
                    id = mention.id;
                    if (mention.url.contains(getDomain(span.getURL()))) {
                        break;
                    }
                }
            }
            if (id != null) {
                final String accountId = id;
                ClickableSpan newSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        listener.onViewAccount(accountId);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setUnderlineText(false);
                    }
                };
                builder.removeSpan(span);
                builder.setSpan(newSpan, start, end, flags);
            }
        } else {
            ClickableSpan newSpan = new CustomURLSpan(span.getURL());
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        }
    }
    view.setText(builder);
    view.setLinksClickable(true);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}