Java String Last Index Of lastIndexOf(String string, String substring)

Here you can find the source of lastIndexOf(String string, String substring)

Description

Reimplementation of lastIndexOf from String class to make it work indepent of the platform used.

License

LGPL

Parameter

Parameter Description
string The String to search
substring The substring to be found inside <code>string</code>

Return

The index of the start of substring in string or -1

Declaration

public static int lastIndexOf(String string, String substring) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//from   ww  w . j av  a2s  . c  o m
     * Reimplementation of <code>lastIndexOf</code> from <code>String</code>
     * class to make it work indepent of the platform used. JavaME's String i.e.
     * does not provide this method.
     *
     * @param string The String to search
     * @param substring The substring to be found inside <code>string</code>
     * @return The index of the start of <code>substring</code> in <code>string</code> or -1
     */
    public static int lastIndexOf(String string, String substring) {
        int len = string.length();

        while (len >= -1) {
            if (string.startsWith(substring, len)) {
                return len;
            }
            len--;
        }
        return len;
    }
}

Related

  1. lastIndexOf(String str, char searchChar)
  2. lastIndexOf(String str, String searchChar)
  3. lastIndexOf(String str, String substr)
  4. lastIndexOf(String string, char value, int startIndex, int count)
  5. lastIndexOf(String string, char... chars)
  6. lastIndexOf(String text, int startPos, String... searchStrings)
  7. lastIndexOf(String text, String key, int num)
  8. lastIndexOf(String what, String within)
  9. lastIndexOf(StringBuffer buf, String str)