Java String Last Index Of lastIndexOf(final String src, final String sub, int startIndex, int endIndex)

Here you can find the source of lastIndexOf(final String src, final String sub, int startIndex, int endIndex)

Description

last Index Of

License

Open Source License

Declaration

public static int lastIndexOf(final String src, final String sub, int startIndex, int endIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int lastIndexOf(final String src, final String sub, int startIndex, int endIndex) {
        final int sublen = sub.length();
        final int srclen = src.length();
        if (sublen == 0) {
            return (startIndex > srclen) ? srclen : ((startIndex < -1) ? -1 : startIndex);
        }//from  ww w . j a v  a2  s  . com
        final int total = srclen - sublen;
        if (total < 0) {
            return -1;
        }
        if (startIndex >= total) {
            startIndex = total;
        }
        if (endIndex < 0) {
            endIndex = 0;
        }
        final char c = sub.charAt(0);
        Label_0144: for (int i = startIndex; i >= endIndex; --i) {
            if (src.charAt(i) == c) {
                for (int j = 1, k = i + 1; j < sublen; ++j, ++k) {
                    if (sub.charAt(j) != src.charAt(k)) {
                        continue Label_0144;
                    }
                }
                return i;
            }
        }
        return -1;
    }

    public static int lastIndexOf(final String src, final char c, int startIndex, int endIndex) {
        final int total = src.length() - 1;
        if (total < 0) {
            return -1;
        }
        if (startIndex >= total) {
            startIndex = total;
        }
        if (endIndex < 0) {
            endIndex = 0;
        }
        for (int i = startIndex; i >= endIndex; --i) {
            if (src.charAt(i) == c) {
                return i;
            }
        }
        return -1;
    }

    public static int[] lastIndexOf(final String s, final String[] arr) {
        return lastIndexOf(s, arr, s.length());
    }

    public static int[] lastIndexOf(final String s, final String[] arr, final int fromIndex) {
        final int arrLen = arr.length;
        int index = -1;
        int last = -1;
        for (int j = 0; j < arrLen; ++j) {
            final int i = s.lastIndexOf(arr[j], fromIndex);
            if (i != -1 && i > index) {
                index = i;
                last = j;
            }
        }
        int[] array;
        if (last == -1) {
            array = null;
        } else {
            final int[] array2 = array = new int[2];
            array2[0] = last;
            array2[1] = index;
        }
        return array;
    }
}

Related

  1. lastIndexOf(CharSequence s, CharSequence seq)
  2. lastIndexOf(CharSequence theChars, CharSequence theSearch)
  3. lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)
  4. lastIndexOf(final CharSequence cs, final int searchChar, int start)
  5. lastIndexOf(final String input, final char delim)
  6. lastIndexOf(String baseString, String indexString)
  7. lastIndexOf(String input, char ch, int offset)
  8. lastIndexOf(String path, char ch)
  9. lastIndexOf(String s, String sub, int startIndex, int endIndex)