Java String Last Index Of lastIndexOf(CharSequence theChars, CharSequence theSearch)

Here you can find the source of lastIndexOf(CharSequence theChars, CharSequence theSearch)

Description

Returns the last index of given search chars in given chars.

License

Open Source License

Declaration

public static int lastIndexOf(CharSequence theChars, CharSequence theSearch) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w ww.  ja v a2  s .  c o m*/
     * Returns the last index of given search chars in given chars.
     */
    public static int lastIndexOf(CharSequence theChars, CharSequence theSearch) {
        int cpos = theChars.length() - theSearch.length() + 1;
        char fchar = theSearch.charAt(0);
        while (--cpos >= 0) {
            if (theChars.charAt(cpos) == fchar) {
                int i = 0;
                for (i = 0; i < theSearch.length(); i++)
                    if (theChars.charAt(cpos + i) != theSearch.charAt(i))
                        break;
                if (i == theSearch.length())
                    return cpos;
            }
        }
        return -1;
    }

    /**
     * Returns the length of given string (supports null).
     */
    public static int length(CharSequence aString) {
        return aString == null ? 0 : aString.length();
    }
}

Related

  1. lastIndexOf(CharSequence charSeq, char ch)
  2. lastIndexOf(CharSequence cs, int searchChar, int start)
  3. lastIndexOf(CharSequence haystack, char needle)
  4. lastIndexOf(CharSequence s, char c, int start, int end)
  5. lastIndexOf(CharSequence s, CharSequence seq)
  6. lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)
  7. lastIndexOf(final CharSequence cs, final int searchChar, int start)
  8. lastIndexOf(final String input, final char delim)
  9. lastIndexOf(final String src, final String sub, int startIndex, int endIndex)