Java String Last Index Of lastIndexOf(CharSequence s, CharSequence seq)

Here you can find the source of lastIndexOf(CharSequence s, CharSequence seq)

Description

Behaves like String.lastIndexOf but for CharSequence.

License

Open Source License

Parameter

Parameter Description
s the string
seq the sequence to test

Return

the index of seq in s or -1 if not found

Declaration

public static int lastIndexOf(CharSequence s, CharSequence seq) 

Method Source Code

//package com.java2s;
/*//  w w  w . ja v  a2s .  com
 * Copyright (c) 2013 Game Salutes.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Contributors:
 *     Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below
 * 
 * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils
 * 
 * Copyright 2008 - 2011 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    /**
     * Behaves like <code>String.lastIndexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param seq the sequence to test
     * @return the index of <code>seq</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int lastIndexOf(CharSequence s, CharSequence seq) {
        return indexOf(s, seq, s.length() - 1, false);
    }

    /**
     * Behaves like <code>String.lastIndexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param seq the sequence to test
     * @param fromIndex the index in <code>s</code> to begin the search
     * @return the index of <code>seq</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int lastIndexOf(CharSequence s, CharSequence seq, int fromIndex) {
        return indexOf(s, seq, fromIndex, false);
    }

    /**
     * Behaves like <code>String.lastIndexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param c the character to test
     * @return the index of <code>c</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int lastIndexOf(CharSequence s, char c) {
        int len = s.length();
        if (len == 0)
            return -1;
        return indexOf(s, c, len - 1, false);
    }

    /**
     * Behaves like <code>String.lastIndexOf</code> but for <code>CharSequence</code>
     * 
     * @param s the string
     * @param c the character to test
     * @param fromIndex the index in <code>s</code> to begin the search
     * @return the index of <code>c</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int lastIndexOf(CharSequence s, char c, int fromIndex) {
        fromIndex = normalizeIndex(s, fromIndex, false);
        if (fromIndex < 0)
            return -1;
        return indexOf(s, c, fromIndex, false);
    }

    /**
     * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param seq the sequence to test
     * @return the index of <code>seq</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int indexOf(CharSequence s, CharSequence seq) {
        return indexOf(s, seq, 0, true);
    }

    /**
     * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param seq the sequence to test
     * @param fromIndex the index in <code>s</code> to begin the search
     * @return the index of <code>seq</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int indexOf(CharSequence s, CharSequence seq, int fromIndex) {
        return indexOf(s, seq, fromIndex, true);
    }

    /**
     * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param c the character to test
     * @return the index of <code>c</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int indexOf(CharSequence s, char c) {
        return indexOf(s, c, 0, true);
    }

    /**
     * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param c the character to test
     * @param fromIndex the index in <code>s</code> to begin the search
     * @return the index of <code>c</code> in <code>s</code> or <code>-1</code> if not found
     */
    public static int indexOf(CharSequence s, char c, int fromIndex) {
        fromIndex = normalizeIndex(s, fromIndex, true);
        if (fromIndex < 0)
            return -1;

        return indexOf(s, c, fromIndex, true);
    }

    private static int indexOf(CharSequence s, char c, int index, boolean fromStart) {
        int len = s.length();
        if (fromStart) {
            for (int i = index; i < len; ++i)
                if (s.charAt(i) == c)
                    return i;
        } else {
            for (int i = index; i >= 0; --i)
                if (s.charAt(i) == c)
                    return i;
        }

        return -1;
    }

    private static int indexOf(CharSequence s, CharSequence seq, int i, boolean fromStart) {
        // do a regional compare starting at begining of string
        if (s == null)
            throw new NullPointerException("s");
        if (seq == null)
            throw new NullPointerException("seq");

        int lenTotal = s.length();
        int lenSeq = seq.length();

        if (lenSeq > lenTotal)
            return -1;
        // s always contains empty string
        if (lenSeq == 0)
            return 0;

        // behave like String.indexOf and String.lastIndexOf
        if (fromStart) {
            if (i < 0)
                i = 0;
            else if (i >= lenTotal)
                return -1;
        } else {
            if (i >= lenTotal)
                i = lenTotal - 1;
            else if (i < 0)
                return -1;
        }

        // look for a match in first character
        int start = i;
        char c = seq.charAt(0);

        while (fromStart ? (start < lenTotal) : (start >= 0)) {
            int index = indexOf(s, c, start, true);

            // no match
            if (index == -1)
                return -1;

            // first character matches: examine rest of sequence
            if (regionMatches(false, s, index, seq, 0, lenSeq))
                return index;

            start = index + (fromStart ? 1 : -1);
        }

        return -1;
    }

    public static int indexOf(boolean ignoreCase, CharSequence thisSeq, int toffset, char c, int len) {
        int to = toffset;
        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((toffset < 0) || (toffset > (long) thisSeq.length() - len)) {
            return -1;
        }

        if (!ignoreCase) {
            while (len-- > 0) {
                char c1 = thisSeq.charAt(to);
                if (c1 == c) {
                    return to;
                }

                ++to;
            }
        } else {
            char uc = Character.toUpperCase(c);
            char lc = Character.toLowerCase(c);

            while (len-- > 0) {
                char c1 = thisSeq.charAt(to);
                if (c1 == c) {
                    return to;
                }
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                if (u1 == uc) {
                    return to;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == lc) {
                    return to;
                }

                ++to;
            }
        }

        return -1;
    }

    private static int normalizeIndex(CharSequence s, int i, boolean fromStart) {
        int len = s.length();

        // behave like String.indexOf and String.lastIndexOf
        if (fromStart) {
            if (i < 0)
                i = 0;
            else if (i >= len)
                return -1;
        } else {
            if (i >= len)
                i = len - 1;
            else if (i < 0)
                return -1;
        }

        return i;
    }

    public static boolean regionMatches(boolean ignoreCase, CharSequence thisSeq, int toffset,
            CharSequence otherSeq, int ooffset, int len) {
        int to = toffset;
        int po = ooffset;
        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((ooffset < 0) || (toffset < 0) || (toffset > (long) thisSeq.length() - len)
                || (ooffset > (long) otherSeq.length() - len)) {
            return false;
        }
        while (len-- > 0) {
            char c1 = thisSeq.charAt(to++);
            char c2 = otherSeq.charAt(po++);
            if (c1 == c2) {
                continue;
            }
            if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2) {
                    continue;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                    continue;
                }
            } // if
            return false;
        } // while

        return true;
    }
}

Related

  1. lastIndexOf(CharSequence chars, String searched)
  2. lastIndexOf(CharSequence charSeq, char ch)
  3. lastIndexOf(CharSequence cs, int searchChar, int start)
  4. lastIndexOf(CharSequence haystack, char needle)
  5. lastIndexOf(CharSequence s, char c, int start, int end)
  6. lastIndexOf(CharSequence theChars, CharSequence theSearch)
  7. lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)
  8. lastIndexOf(final CharSequence cs, final int searchChar, int start)
  9. lastIndexOf(final String input, final char delim)