Java String Last Index Of lastIndexOf(final String input, final char delim)

Here you can find the source of lastIndexOf(final String input, final char delim)

Description

Gets the last index of a character ignoring characters that have been escaped

License

Open Source License

Parameter

Parameter Description
input The string to be searched
delim The character to be found

Return

The index of the found character or -1 if the character wasn't found

Declaration

public static int lastIndexOf(final String input, final char delim) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  w w .  j a v  a 2s  . co  m*/
     * Gets the last index of a character ignoring characters that have been escaped
     *
     * @param input The string to be searched
     * @param delim The character to be found
     * @return The index of the found character or -1 if the character wasn't found
     */
    public static int lastIndexOf(final String input, final char delim) {
        return input == null ? -1 : lastIndexOf(input, delim, input.length());
    }

    /**
     * Gets the last index of a character starting at fromIndex. Ignoring characters that have been escaped
     *
     * @param input     The string to be searched
     * @param delim     The character to be found
     * @param fromIndex Start searching from this index
     * @return The index of the found character or -1 if the character wasn't found
     */
    public static int lastIndexOf(final String input, final char delim, final int fromIndex) {
        if (input == null)
            return -1;
        int index = input.lastIndexOf(delim, fromIndex);
        while (index != -1 && index != 0) {
            if (input.charAt(index - 1) != '\\')
                break;
            index = input.lastIndexOf(delim, index - 1);
        }
        return index;
    }
}

Related

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