Android examples for java.lang:String Search
Finds the index of the first word that starts with the given prefix.
import android.text.TextUtils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ /**/*from w w w .ja v a2 s .c om*/ * 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. */ 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; } }