Java String Last Index Of lastIndexOf(CharSequence s, char c, int start, int end)

Here you can find the source of lastIndexOf(CharSequence s, char c, int start, int end)

Description

Returns the index within this string of the last occurrence of the specified char, searching in specified range

License

Open Source License

Declaration

public static int lastIndexOf(CharSequence s, char c, int start, int end) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012-2017 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from ww  w.j ava  2 s . c o  m
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Returns the index within this string of the last occurrence of the
     * specified char, searching in specified range
     */
    public static int lastIndexOf(CharSequence s, char c, int start, int end) {
        start = Math.max(start, 0);
        for (int i = Math.min(end, s.length()) - 1; i >= start; i--) {
            if (s.charAt(i) == c) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. lastIndexof(char chr, int pos, CharSequence str)
  2. lastIndexOf(CharSequence chars, String searched)
  3. lastIndexOf(CharSequence charSeq, char ch)
  4. lastIndexOf(CharSequence cs, int searchChar, int start)
  5. lastIndexOf(CharSequence haystack, char needle)
  6. lastIndexOf(CharSequence s, CharSequence seq)
  7. lastIndexOf(CharSequence theChars, CharSequence theSearch)
  8. lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)
  9. lastIndexOf(final CharSequence cs, final int searchChar, int start)