Java String Last Index Of lastIndexOf(String string, char... chars)

Here you can find the source of lastIndexOf(String string, char... chars)

Description

last Index Of

License

Open Source License

Parameter

Parameter Description
string a parameter
chars a parameter

Declaration

public static int lastIndexOf(String string, char... chars) 

Method Source Code

//package com.java2s;
/**// w w w  .ja v a 2 s . c o m
 * Aptana Studio
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

public class Main {
    /**
     * @see String#lastIndexOf(int)
     * @param string
     * @param chars
     * @return
     */
    public static int lastIndexOf(String string, char... chars) {
        if (string == null) {
            return -1;
        }
        return lastIndexOf(string, string.length() - 1, chars);
    }

    /**
     * @see String#lastIndexOf(String, int)
     * @param string
     * @param fromIndex
     * @param chars
     * @return
     */
    public static int lastIndexOf(String string, int fromIndex, char... chars) {
        if (chars == null || chars.length == 0) {
            return -1;
        }
        int length = string.length();
        if (length == 0) {
            return -1;
        } else if (fromIndex >= length) {
            fromIndex = length - 1;
        }

        for (int i = fromIndex; i >= 0; i--) {
            char c = string.charAt(i);
            for (char x : chars) {
                if (c == x) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

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