Java String Last Index Of lastIndexOf(String string, char value, int startIndex, int count)

Here you can find the source of lastIndexOf(String string, char value, int startIndex, int count)

Description

last Index Of

License

Open Source License

Declaration

public static int lastIndexOf(String string, char value, int startIndex, int count) 

Method Source Code

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

public class Main {
    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/*  w  ww  .jav  a  2 s.com*/
            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. lastIndexOf(String self, String pattern, Integer start)
  2. lastIndexOf(String str, char searchChar)
  3. lastIndexOf(String str, char searchChar)
  4. lastIndexOf(String str, String searchChar)
  5. lastIndexOf(String str, String substr)
  6. lastIndexOf(String string, char... chars)
  7. lastIndexOf(String string, String substring)
  8. lastIndexOf(String text, int startPos, String... searchStrings)
  9. lastIndexOf(String text, String key, int num)