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:com.anysoftkeyboard.AnySoftKeyboard.java

private void handleBackWord(InputConnection ic) {
    if (ic == null) {
        return;//from  ww  w .  j a v  a 2 s  .  co m
    }

    if (TextEntryState.isPredicting() && mWord.cursorPosition() > 0 && mWord.length() > 0) {
        //sp#ace -> ace
        //cursor == 2
        //length == 5
        //textLeft = word.substring(2, 3) -> word.substring(cursor, length - cursor)
        final CharSequence textLeft = mWord.getTypedWord().subSequence(mWord.cursorPosition(), mWord.length());
        mWord.reset();
        mSuggest.resetNextWordSentence();
        TextEntryState.newSession(mPredictionOn);
        ic.setComposingText(textLeft, 0);
        postUpdateSuggestions();
        return;
    }
    // I will not delete more than 128 characters. Just a safe-guard.
    // this will also allow me do just one call to getTextBeforeCursor!
    // Which is always good. This is a part of issue 951.
    CharSequence cs = ic.getTextBeforeCursor(128, 0);
    if (TextUtils.isEmpty(cs)) {
        return;// nothing to delete
    }
    // TWO OPTIONS
    // 1) Either we do like Linux and Windows (and probably ALL desktop
    // OSes):
    // Delete all the characters till a complete word was deleted:
    /*
     * What to do: We delete until we find a separator (the function
     * isBackWordStopChar). Note that we MUST delete a delete a whole word!
     * So if the back-word starts at separators, we'll delete those, and then
     * the word before: "test this,       ," -> "test "
     */
    // Pro: same as desktop
    // Con: when auto-caps is on (the default), this will delete the
    // previous word, which can be annoying..
    // E.g., Writing a sentence, then a period, then ASK will auto-caps,
    // then when the user press backspace (for some reason),
    // the entire previous word deletes.

    // 2) Or we delete all the characters till we encounter a separator, but
    // delete at least one character.
    /*
     * What to do: We delete until we find a separator (the function
     * isBackWordStopChar). Note that we MUST delete a delete at least one
     * character "test this, " -> "test this," -> "test this" -> "test "
     */
    // Pro: Supports auto-caps, and mostly similar to desktop OSes
    // Con: Not all desktop use-cases are here.

    // For now, I go with option 2, but I'm open for discussion.

    // 2b) "test this, " -> "test this"

    final int inputLength = cs.length();
    int idx = inputLength - 1;// it's OK since we checked whether cs is empty after retrieving it.
    if (isBackWordDeleteChar((int) cs.charAt(idx))) {
        while (idx > 0 && isBackWordDeleteChar((int) cs.charAt(idx - 1))) {
            idx--;
        }
    }
    ic.deleteSurroundingText(inputLength - idx, 0);// it is always > 0 !
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

public void pickSuggestionManually(int index, CharSequence suggestion) {
    List<CharSequence> suggestions = mCandidateView.getSuggestions();

    final boolean correcting = TextEntryState.isCorrecting();
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.beginBatchEdit();/* www. jav  a  2  s  .c  o  m*/
    }
    if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
        CompletionInfo ci = mCompletions[index];
        if (ic != null) {
            ic.commitCompletion(ci);
        }
        mCommittedLength = suggestion.length();
        if (mCandidateView != null) {
            mCandidateView.clear();
        }
        updateShiftKeyState(getCurrentInputEditorInfo());
        if (ic != null) {
            ic.endBatchEdit();
        }
        return;
    }

    // If this is a punctuation, apply it through the normal key press
    if (suggestion.length() == 1
            && (isWordSeparator(suggestion.charAt(0)) || isSuggestedPunctuation(suggestion.charAt(0)))) {
        // Word separators are suggested before the user inputs something.
        // So, TKIMELogger logs "" as a user's input.
        TKIMELogger.logOnManualSuggestion("", suggestion.toString(), index, suggestions);
        final char primaryCode = suggestion.charAt(0);
        onKey(primaryCode, new int[] { primaryCode }, BaseKeyboardView.NOT_A_TOUCH_COORDINATE,
                BaseKeyboardView.NOT_A_TOUCH_COORDINATE);
        if (ic != null) {
            ic.endBatchEdit();
        }
        return;
    }
    mJustAccepted = true;
    pickSuggestion(suggestion, correcting);
    // Add the word to the auto dictionary if it's not a known word
    if (index == 0) {
        addToDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED);
    } else {
        addToBigramDictionary(suggestion, 1);
    }
    TKIMELogger.logOnManualSuggestion(mComposing.toString(), suggestion.toString(), index, suggestions);
    TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion);
    // Follow it with a space
    if (mAutoSpace && !correcting) {
        sendSpace();
        mJustAddedAutoSpace = true;
    }

    final boolean showingAddToDictionaryHint = index == 0 && mCorrectionMode > 0
            && !mSuggest.isValidWord(suggestion) && !mSuggest.isValidWord(suggestion.toString().toLowerCase());

    if (!correcting) {
        // Fool the state watcher so that a subsequent backspace will not do
        // a revert, unless
        // we just did a correction, in which case we need to stay in
        // TextEntryState.State.PICKED_SUGGESTION state.
        TextEntryState.typedCharacter((char) ASCII_SPACE, true);
        setNextSuggestions();
    } else if (!showingAddToDictionaryHint) {
        // If we're not showing the "Touch again to save", then show
        // corrections again.
        // In case the cursor position doesn't change, make sure we show the
        // suggestions again.
        clearSuggestions();
        postUpdateOldSuggestions();
    }
    if (showingAddToDictionaryHint) {
        mCandidateView.showAddToDictionaryHint(suggestion);
    }
    if (ic != null) {
        ic.endBatchEdit();
    }
}

From source file:org.regenstrief.util.Util.java

public final static int fromHex(final CharSequence s, final int off, final int len) {
    int r = 0;//from  w w w. ja  va  2  s.com
    for (int i = off; i < len; i++) {
        r = (r * 16) + fromHex(s.charAt(i));
    }
    return r;
}

From source file:org.regenstrief.util.Util.java

/**
 * Retrieves the number of occurrences of the given character
 * /*from  ww w  .j a  v a  2s . c om*/
 * @param src the source CharSequence
 * @param c the character
 * @return the number of occurrences
 **/
public final static int count(final CharSequence src, final char c) {
    final int size = length(src);
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (src.charAt(i) == c) {
            count++;
        }
    }
    return count;
}

From source file:org.regenstrief.util.Util.java

/**
 * Retrieves the index of the first different character between two CharSequences
 * //from ww w .j a v a 2 s. c  om
 * @param s1 the first CharSequence
 * @param s2 the second CharSequence
 * @return the index of the first different character (or -1 if they are identical)
 **/
public final static int diff(final CharSequence s1, final CharSequence s2) {
    final int len1 = s1.length(), len2 = s2.length(), len = Math.min(len1, len2);

    for (int i = 0; i < len; i++) {
        if (s1.charAt(i) != s2.charAt(i)) {
            return i;
        }
    }

    return len1 == len2 ? -1 : len;
}

From source file:org.regenstrief.util.Util.java

/**
 * Appends part of the given CharSequence to the given StringBuffer, allocating the StringBuffer
 * if necessary/*from  w  w w.j  a  va 2 s  . c  o m*/
 * 
 * @param sb the StringBuffer
 * @param s the String
 * @param offset the offset of the CharSequence at which to start copying
 * @param len the number of characters to copy
 * @return the StringBuffer
 **/
public final static StringBuffer append(StringBuffer sb, final CharSequence s, int offset, int len) {
    if (sb == null) {
        sb = new StringBuffer(len);
    }
    sb.ensureCapacity(sb.length() + len);
    for (len += offset; offset < len; offset++) {
        sb.append(s.charAt(offset));
    }

    return sb;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode digits or space
 * ({@code ' '})./*from  w w  w .ja  va 2  s.  c o  m*/
 * A decimal point is not a Unicode digit and returns false.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>
 * StringUtils.isNumericSpace(null)   = false
 * StringUtils.isNumericSpace("")     = true
 * StringUtils.isNumericSpace("  ")   = true
 * StringUtils.isNumericSpace("123")  = true
 * StringUtils.isNumericSpace("12 3") = true
 * StringUtils.isNumericSpace("ab2c") = false
 * StringUtils.isNumericSpace("12-3") = false
 * StringUtils.isNumericSpace("12.3") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains digits or space,
 *  and is non-null
 * @since 3.0 Changed signature from isNumericSpace(String) to isNumericSpace(CharSequence)
 */
public static boolean isNumericSpace(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isDigit(cs.charAt(i)) == false) && (cs.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode letters and
 * space (' ').</p>/*from   w ww . j  a va  2 s  .  c o  m*/
 *
 * <p>{@code null} will return {@code false}
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>
 * StringUtils.isAlphaSpace(null)   = false
 * StringUtils.isAlphaSpace("")     = true
 * StringUtils.isAlphaSpace("  ")   = true
 * StringUtils.isAlphaSpace("abc")  = true
 * StringUtils.isAlphaSpace("ab c") = true
 * StringUtils.isAlphaSpace("ab2c") = false
 * StringUtils.isAlphaSpace("ab-c") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains letters and space,
 *  and is non-null
 * @since 3.0 Changed signature from isAlphaSpace(String) to isAlphaSpace(CharSequence)
 */
public static boolean isAlphaSpace(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isLetter(cs.charAt(i)) == false) && (cs.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only whitespace.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>//w  w w.j ava 2s. c  o m
 * StringUtils.isWhitespace(null)   = false
 * StringUtils.isWhitespace("")     = true
 * StringUtils.isWhitespace("  ")   = true
 * StringUtils.isWhitespace("abc")  = false
 * StringUtils.isWhitespace("ab2c") = false
 * StringUtils.isWhitespace("ab-c") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains whitespace, and is non-null
 * @since 2.0
 * @since 3.0 Changed signature from isWhitespace(String) to isWhitespace(CharSequence)
 */
public static boolean isWhitespace(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isWhitespace(cs.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode letters, digits
 * or space ({@code ' '}).</p>//from w  ww.j ava 2 s. co  m
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>
 * StringUtils.isAlphanumericSpace(null)   = false
 * StringUtils.isAlphanumericSpace("")     = true
 * StringUtils.isAlphanumericSpace("  ")   = true
 * StringUtils.isAlphanumericSpace("abc")  = true
 * StringUtils.isAlphanumericSpace("ab c") = true
 * StringUtils.isAlphanumericSpace("ab2c") = true
 * StringUtils.isAlphanumericSpace("ab-c") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains letters, digits or space,
 *  and is non-null
 * @since 3.0 Changed signature from isAlphanumericSpace(String) to isAlphanumericSpace(CharSequence)
 */
public static boolean isAlphanumericSpace(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isLetterOrDigit(cs.charAt(i)) == false) && (cs.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}