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

public static boolean isCharAtEqual(CharSequence source, int index, char match) {
    if ((index < 0) || (index >= source.length())) {
        return false;
    }/*from w  w  w  . jav a 2s.c om*/
    return source.charAt(index) == match;
}

From source file:eu.stratosphere.types.StringValue.java

/**
 * Finds any occurrence of the <code>str</code> character sequence in this StringValue.
 * The search starts at position <code>start</code>.
 * /* w w  w .  j a v a 2  s .  c  om*/
 * @return The position of the first occurrence of the search string in the string value, or <code>-1</code>, if
 *         the character sequence was not found.
 */
public int find(CharSequence str, int start) {
    final int pLen = this.len;
    final int sLen = str.length();

    if (sLen == 0) {
        throw new IllegalArgumentException("Cannot find empty string.");
    }

    int pPos = start;

    final char first = str.charAt(0);

    while (pPos < pLen) {
        if (first == this.value[pPos++]) {
            // matching first character
            final int fallBackPosition = pPos;
            int sPos = 1;
            boolean found = true;

            while (sPos < sLen) {
                if (pPos >= pLen) {
                    // no more characters in string value
                    pPos = fallBackPosition;
                    found = false;
                    break;
                }

                if (str.charAt(sPos++) != this.value[pPos++]) {
                    pPos = fallBackPosition;
                    found = false;
                    break;
                }
            }
            if (found) {
                return fallBackPosition - 1;
            }
        }
    }
    return -1;
}

From source file:net.iiit.siel.analysis.lang.LanguageIdentifier.java

/**
 * Check char sequence.//from w w  w  . java 2  s.  c  om
 *
 * @param seq the seq
 * @return the boolean
 */
private Boolean checkCharSequence(CharSequence seq) {

    Boolean isForeign = true;
    char underScore = '_';
    for (int j = 0; j < seq.length(); j++) {
        if (seq.charAt(j) == underScore) {
            continue;
        }
        UnicodeBlock currentUnicode = Character.UnicodeBlock.of(seq.charAt(j));

        if (currentUnicode == Character.UnicodeBlock.BENGALI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.TELUGU)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.TAMIL)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.DEVANAGARI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.GURMUKHI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.GUJARATI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.ORIYA)
            isForeign = false;
        else if (seq.charAt(j) >= 0 && seq.charAt(j) <= 127)
            isForeign = false;
        /*
        *                          * Return if it is a foreign character, because seq can be composed
        *                                                   * of indic and foreign characters.
        *                                                                            */
        else
            return isForeign;
    }
    return isForeign;
}

From source file:eu.stratosphere.types.StringValue.java

/**
 * Sets the value of the StringValue to a substring of the given string.
 * //from  ww  w.  j a  va 2 s  .  c o  m
 * @param value The new string value.
 * @param offset The position to start the substring.
 * @param len The length of the substring.
 */
public void setValue(CharSequence value, int offset, int len) {
    Validate.notNull(value);
    if (offset < 0 || len < 0 || offset > value.length() - len) {
        throw new IndexOutOfBoundsException("offset: " + offset + " len: " + len + " value.len: " + len);
    }

    ensureSize(len);
    this.len = len;
    for (int i = 0; i < len; i++) {
        this.value[i] = value.charAt(offset + i);
    }
    this.len = len;
    this.hashCode = 0;
}

From source file:com.xorcode.andtweet.TweetListActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    super.onContextItemSelected(item);
    AdapterView.AdapterContextMenuInfo info;
    try {//  w w w  . j a va2 s  . com
        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
        return false;
    }

    mCurrentId = info.id;

    Uri uri;
    Cursor c;

    switch (item.getItemId()) {
    case CONTEXT_MENU_ITEM_REPLY:
        uri = ContentUris.withAppendedId(Tweets.CONTENT_URI, info.id);
        c = getContentResolver().query(uri, new String[] { Tweets._ID, Tweets.AUTHOR_ID }, null, null, null);
        try {
            c.moveToFirst();
            String reply = "@" + c.getString(c.getColumnIndex(Tweets.AUTHOR_ID)) + " ";
            long replyId = c.getLong(c.getColumnIndex(Tweets._ID));
            mTweetEditor.startEditing(reply, replyId);
        } catch (Exception e) {
            Log.e(TAG, "onContextItemSelected: " + e.toString());
            return false;
        } finally {
            if (c != null && !c.isClosed())
                c.close();
        }
        return true;

    case CONTEXT_MENU_ITEM_RETWEET:
        uri = ContentUris.withAppendedId(Tweets.CONTENT_URI, info.id);
        c = getContentResolver().query(uri, new String[] { Tweets._ID, Tweets.AUTHOR_ID, Tweets.MESSAGE }, null,
                null, null);
        try {
            c.moveToFirst();

            StringBuilder message = new StringBuilder();
            String reply = "RT @" + c.getString(c.getColumnIndex(Tweets.AUTHOR_ID)) + " ";
            message.append(reply);
            CharSequence text = c.getString(c.getColumnIndex(Tweets.MESSAGE));
            int len = 140 - reply.length() - 3;
            if (text.length() < len) {
                len = text.length();
            }
            message.append(text, 0, len);
            if (message.length() == 137) {
                message.append("...");
            }
            mTweetEditor.startEditing(message.toString(), 0);
        } catch (Exception e) {
            Log.e(TAG, "onContextItemSelected: " + e.toString());
            return false;
        } finally {
            if (c != null && !c.isClosed())
                c.close();
        }
        return true;

    case CONTEXT_MENU_ITEM_DESTROY_STATUS:
        sendCommand(new CommandData(CommandEnum.DESTROY_STATUS, mCurrentId));
        return true;

    case CONTEXT_MENU_ITEM_FAVORITE:
        sendCommand(new CommandData(CommandEnum.CREATE_FAVORITE, mCurrentId));
        return true;

    case CONTEXT_MENU_ITEM_DESTROY_FAVORITE:
        sendCommand(new CommandData(CommandEnum.DESTROY_FAVORITE, mCurrentId));
        return true;

    case CONTEXT_MENU_ITEM_SHARE:
        uri = ContentUris.withAppendedId(Tweets.CONTENT_URI, info.id);
        c = getContentResolver().query(uri, new String[] { Tweets._ID, Tweets.AUTHOR_ID, Tweets.MESSAGE }, null,
                null, null);
        try {
            c.moveToFirst();

            StringBuilder subject = new StringBuilder();
            StringBuilder text = new StringBuilder();
            String message = c.getString(c.getColumnIndex(Tweets.MESSAGE));

            subject.append(getText(R.string.button_create_tweet));
            subject.append(" - " + message);
            int maxlength = 80;
            if (subject.length() > maxlength) {
                subject.setLength(maxlength);
                // Truncate at the last space
                subject.setLength(subject.lastIndexOf(" "));
                subject.append("...");
            }

            text.append(message);
            text.append("\n-- \n" + c.getString(c.getColumnIndex(Tweets.AUTHOR_ID)));
            text.append("\n URL: " + "http://twitter.com/" + c.getString(c.getColumnIndex(Tweets.AUTHOR_ID))
                    + "/status/" + c.getString(c.getColumnIndex(Tweets._ID)));

            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("text/plain");
            share.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
            share.putExtra(Intent.EXTRA_TEXT, text.toString());
            startActivity(Intent.createChooser(share, getText(R.string.menu_item_share)));

        } catch (Exception e) {
            Log.e(TAG, "onContextItemSelected: " + e.toString());
            return false;
        } finally {
            if (c != null && !c.isClosed())
                c.close();
        }
        return true;

    case CONTEXT_MENU_ITEM_UNFOLLOW:
    case CONTEXT_MENU_ITEM_BLOCK:
    case CONTEXT_MENU_ITEM_DIRECT_MESSAGE:
    case CONTEXT_MENU_ITEM_PROFILE:
        Toast.makeText(this, R.string.unimplemented, Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}

From source file:com.intellij.lang.jsgraphql.ide.annotator.JSGraphQLAnnotator.java

private CharSequence getWhitespacePaddedGraphQL(PsiFile psiFile, CharSequence buffer) {
    // find the template expressions in the file
    Collection<JSStringTemplateExpression> stringTemplateExpressions = PsiTreeUtil
            .collectElementsOfType(psiFile, JSStringTemplateExpression.class);
    StringBuilder sb = new StringBuilder(0);
    Integer builderPos = null;//from   w  w w.j av a 2 s.  c  o  m
    for (JSStringTemplateExpression stringTemplateExpression : stringTemplateExpressions) {
        if (JSGraphQLLanguageInjectionUtil.isJSGraphQLLanguageInjectionTarget(stringTemplateExpression)) {
            final TextRange graphQLTextRange = JSGraphQLLanguageInjectionUtil
                    .getGraphQLTextRange(stringTemplateExpression);
            if (builderPos == null) {
                sb.setLength(buffer.length());
                builderPos = 0;
            }
            // write the JS as whitespace so it'll be ignored by the GraphQL tooling, while preserving line numbers and columns.
            TextRange templateTextRange = stringTemplateExpression.getTextRange();
            int graphQLStartOffset = templateTextRange.getStartOffset() + graphQLTextRange.getStartOffset();
            int graphQLEndOffset = templateTextRange.getStartOffset() + graphQLTextRange.getEndOffset();
            applyWhiteSpace(buffer, sb, builderPos, graphQLStartOffset);
            String graphQLText = buffer.subSequence(graphQLStartOffset, graphQLEndOffset /* end is exclusive*/)
                    .toString();
            sb.replace(graphQLStartOffset, graphQLEndOffset /* end is exclusive*/, graphQLText);
            builderPos = graphQLEndOffset /* start next whitespace padding after the graph ql */;
        }
    }

    // last whitespace segment
    if (builderPos != null && builderPos < buffer.length()) {
        applyWhiteSpace(buffer, sb, builderPos, buffer.length());
    }

    return sb;
}

From source file:eu.stratosphere.types.StringValue.java

/**
 * Checks whether the substring, starting at the specified index, starts with the given prefix string.
 * //from w ww  .j  a v  a 2 s . c o  m
 * @param prefix The prefix character sequence.
 * @param startIndex The position to start checking for the prefix.
 * 
 * @return True, if this StringValue substring, starting at position <code>startIndex</code> has </code>prefix</code>
 *         as its prefix.
 */
public boolean startsWith(CharSequence prefix, int startIndex) {
    final char[] thisChars = this.value;
    final int pLen = this.len;
    final int sLen = prefix.length();

    if ((startIndex < 0) || (startIndex > pLen - sLen)) {
        return false;
    }

    int sPos = 0;
    while (sPos < sLen) {
        if (thisChars[startIndex++] != prefix.charAt(sPos++)) {
            return false;
        }
    }
    return true;
}

From source file:android.support.design.widget.CollapsingTextHelper.java

private boolean calculateIsRtl(CharSequence text) {
    final boolean defaultIsRtl = ViewCompat.getLayoutDirection(mView) == ViewCompat.LAYOUT_DIRECTION_RTL;
    return (defaultIsRtl ? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
            : TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR).isRtl(text, 0, text.length());
}

From source file:android.hqs.view.pager.indicator.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index/*from  w w  w.ja  va2s.c o m*/
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}

From source file:StringUtil.java

/**
 * Converts char sequence into byte array. Chars are truncated to byte size.
 *///from  w  w  w.  j  a v a 2s .c  om
public static byte[] toByteArray(CharSequence charSequence) {
    if (charSequence == null) {
        return null;
    }
    byte[] barr = new byte[charSequence.length()];
    for (int i = 0; i < barr.length; i++) {
        barr[i] = (byte) charSequence.charAt(i);
    }
    return barr;
}