Java Array Last Index Of lastIndexOfAny(String string, char[] anyOf)

Here you can find the source of lastIndexOfAny(String string, char[] anyOf)

Description

last Index Of Any

License

Open Source License

Declaration

public static int lastIndexOfAny(String string, char[] anyOf) 

Method Source Code

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

public class Main {
    public static int lastIndexOfAny(String string, char[] anyOf) {
        int highestIndex = -1;
        for (char c : anyOf) {
            int index = string.lastIndexOf(c);
            if (index > highestIndex) {
                highestIndex = index;/*  ww w.ja  va2  s  . co  m*/

                if (index == string.length() - 1)
                    break;
            }
        }

        return highestIndex;
    }

    public static int lastIndexOfAny(String string, char[] anyOf, int startIndex) {
        String substring = string.substring(0, startIndex + 1);
        int lastIndexInSubstring = lastIndexOfAny(substring, anyOf);
        if (lastIndexInSubstring < 0)
            return -1;
        else
            return lastIndexInSubstring;
    }

    public static int lastIndexOfAny(String string, char[] anyOf, int startIndex, int count) {
        int leftMost = startIndex + 1 - count;
        int rightMost = startIndex + 1;
        String substring = string.substring(leftMost, rightMost);
        int lastIndexInSubstring = lastIndexOfAny(substring, anyOf);
        if (lastIndexInSubstring < 0)
            return -1;
        else
            return lastIndexInSubstring + leftMost;
    }

    public static int lastIndexOf(String string, char value, int startIndex, int count) {
        int leftMost = startIndex + 1 - count;
        int rightMost = startIndex + 1;
        String substring = string.substring(leftMost, rightMost);
        int lastIndexInSubstring = substring.lastIndexOf(value);
        if (lastIndexInSubstring < 0)
            return -1;
        else
            return lastIndexInSubstring + leftMost;
    }

    public static int lastIndexOf(String string, String value, int startIndex, int count) {
        int leftMost = startIndex + 1 - count;
        int rightMost = startIndex + 1;
        String substring = string.substring(leftMost, rightMost);
        int lastIndexInSubstring = substring.lastIndexOf(value);
        if (lastIndexInSubstring < 0)
            return -1;
        else
            return lastIndexInSubstring + leftMost;
    }

    public static String substring(String string, int start, int length) {
        if (length < 0)
            throw new IndexOutOfBoundsException("Parameter length cannot be negative.");

        return string.substring(start, start + length);
    }
}

Related

  1. lastIndexOfAny(String str, char[] searchChars, int startPos)
  2. lastIndexOfAny(String str, char[] targets)
  3. lastIndexOfAny(String str, String[] searchStrs)
  4. lastIndexOfAny(String str, String[] searchStrs)
  5. lastIndexOfAny(String str, String[] searchStrs)
  6. lastIndexOfAnyNoCheck(byte[] values, byte[] array, int index, int length)
  7. lastIndexOfArray(int r[], int rpos, int rend, char d[], int dpos)
  8. lastIndexOfChars(String src, char[] chars, int startIndex, int endIndex)
  9. lastIndexOfInsensitive(String o, String[] vals)