app/src/main/java/com/maijia/rc/utils/StringUtil.java
public static int indexOfWordPrefix(CharSequence text, char[] prefix) {
        if (prefix == null || text == null) {
            return -1;
        }

        int textLength = text.length();
        int prefixLength = prefix.length;

        if (prefixLength == 0 || textLength < prefixLength) {
            return -1;
        }

        int i = 0;
        while (i < textLength) {
            // Skip non-word characters
            while (i < textLength && !Character.isLetterOrDigit(text.charAt(i))) {
                i++;
            }

            if (i + prefixLength > textLength) {
                return -1;
            }

            // Compare the prefixes
            int j;
            for (j = 0; j < prefixLength; j++) {
                if (Character.toUpperCase(text.charAt(i + j)) != prefix[j]) {
                    break;
                }
            }
            if (j == prefixLength) {
                return i;
            }
            // Skip this word
            while (i < textLength && Character.isLetterOrDigit(text.charAt(i))) {
                i++;
            }
        }
        return -1;
    }
/**
     * Finds the index of the first word that starts with the given prefix.
     * <p/>
     *
     * @param text   the text in which to search for the prefix
     * @param prefix the text to find, in upper case letters
     * @return index If not found,returns -1.
     */
Finds the index of the first word that starts with the given prefix.

@param text   the text in which to search for the prefix
@param prefix the text to find, in upper case letters
@return index If not found,returns -1.
Finds the index of the first word that starts with the given prefix . @param text the text in which to search for the prefix @param prefix the text to find in upper case letters @return index If not found returns - 1 .
public static int indexOfWordPrefix(CharSequence text, char[] prefix) {
        if (prefix == null || text == null) {
            return -1;
        }
        int textLength = text.length();
        int prefixLength = prefix.length;
        if (prefixLength == 0 || textLength < prefixLength) {
            return -1;
        }
        int i = 0;
        while (i < textLength) {
            while (i < textLength && !Character.isLetterOrDigit(text.charAt(i))) {
                i++;
            }
            if (i + prefixLength > textLength) {
                return -1;
            }
            int j;
            for (j = 0; j < prefixLength; j++) {
                if (Character.toUpperCase(text.charAt(i + j)) != prefix[j]) {
                    break;
                }
            }
            if (j == prefixLength) {
                return i;
            }
            while (i < textLength && Character.isLetterOrDigit(text.charAt(i))) {
                i++;
            }
        }
        return -1;
    }
public static int indexOfWordPrefix ( CharSequence text , char [ ] prefix ) { if ( prefix == null || text == null ) { return - 1 ; } int textLength = text . length ( ) ; int prefixLength = prefix . length ; if ( prefixLength == 0 || textLength < prefixLength ) { return - 1 ; } int i = 0 ; while ( i < textLength ) { while ( i < textLength && ! Character . isLetterOrDigit ( text . charAt ( i ) ) ) { i ++ ; } if ( i + prefixLength > textLength ) { return - 1 ; } int j ; for ( j = 0 ; j < prefixLength ; j ++ ) { if ( Character . toUpperCase ( text . charAt ( i + j ) ) != prefix [ j ] ) { break ; } } if ( j == prefixLength ) { return i ; } while ( i < textLength && Character . isLetterOrDigit ( text . charAt ( i ) ) ) { i ++ ; } } return - 1 ; }
Finds the index of the first word that starts with the given prefix.
Finds the index of the first word that starts with the given prefix .
// Skip non-word characters // Compare the prefixes // Skip this word