find Last Word Index - Android java.lang

Android examples for java.lang:String Search

Description

find Last Word Index

Demo Code

public class Main {
  public static final String WHITESPACE = "\\s+";

  public static int findLastWordIndex(CharSequence sentence) {
    boolean lastWordFound = false;
    for (int i = sentence.length() - 1; i >= 0; i--) {
      if (String.valueOf(sentence.charAt(i)).matches(WHITESPACE)) {
        if (lastWordFound) {
          return i + 1;
        }//from   ww  w  . j  a  va2  s. co m
      } else {
        lastWordFound = true;
      }
    }
    return 0;
  }

}

Related Tutorials