Java String Last Index Of lastIndexOfOrdinal(String value, String part, int startIndex, int count, boolean caseSensitive)

Here you can find the source of lastIndexOfOrdinal(String value, String part, int startIndex, int count, boolean caseSensitive)

Description

Performs an ordinal string comparison.

License

Open Source License

Parameter

Parameter Description
value The source value
part The value we're looking for
startIndex The index in source where we start looking
count Up to how many positions to look forward
caseSensitive Whether case sensitive or not

Exception

Parameter Description
NullPointerException An argument is null.
IndexOutOfBoundsException An index is out of bounds
IllegalArgumentException An argument is out of range

Return

The index where the part was found in value, or -1 if not found.

Declaration

private static int lastIndexOfOrdinal(String value, String part, int startIndex, int count,
        boolean caseSensitive) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU Lesser General Public License as published by

public class Main {
    /**//from  w  w w.  ja v  a 2s. c  o  m
     * Performs an ordinal string comparison.
     * 
     * @param value The source value
     * @param part The value we're looking for
     * @param startIndex The index in source where we start looking
     * @param count Up to how many positions to look forward
     * @param caseSensitive Whether case sensitive or not
     * 
     * @return The index where the part was found in value, or -1 if not found.
     * 
     * @throws NullPointerException An argument is null.
     * @throws IndexOutOfBoundsException An index is out of bounds
     * @throws IllegalArgumentException An argument is out of range
     */
    private static int lastIndexOfOrdinal(String value, String part, int startIndex, int count,
            boolean caseSensitive) {
        int valueLen = value.length();
        int partLen = part.length();
        int endIndex = startIndex - count + 1;

        if ((valueLen == 0) && (startIndex == -1 || startIndex == 0))
            if (partLen != 0)
                return -1;
            else
                return 0;

        if ((startIndex < 0) || (startIndex > valueLen))
            throw new IndexOutOfBoundsException("startIndex=" + startIndex + " valueLen=" + valueLen);

        // following commented out: replaced by this
        if ((count < 0) || endIndex < 0)
            throw new IllegalArgumentException("count=" + count + " startIndex=" + startIndex);

        // calculate the least amount of time we have to iterate through string characters
        int minCount = startIndex - (valueLen - partLen);
        if (endIndex > minCount)
            minCount = endIndex;

        char[] vArr = value.toCharArray();
        char[] pArr = part.toCharArray();

        if (caseSensitive)
            for (int i = startIndex; i >= minCount; i--) {
                boolean found = true;
                for (int j = 0; j < partLen; j++)
                    if (i - j < 0)
                        found = false;
                    else {
                        char vCh = vArr[i - j];
                        char pCh = pArr[partLen - j - 1];
                        // compare chars
                        if (vCh != pCh) {
                            found = false;
                            break;
                        }
                    }

                if (found)
                    return i - partLen + 1;
            }
        else
            // case insensitive
            for (int i = startIndex; i >= minCount; i--) {
                boolean found = true;
                for (int j = 0; j < partLen; j++)
                    if (i - j < 0)
                        found = false;
                    else {
                        char vCh = vArr[i - j];
                        char pCh = pArr[partLen - j - 1];

                        // ASCII letters are converted to lowercase
                        if ((vCh >= 65 && vCh <= 90) || (vCh >= 97 && vCh <= 122)) {
                            vCh = Character.toLowerCase(vCh);
                            pCh = Character.toLowerCase(pCh);
                        }
                        // compare chars
                        if (vCh != pCh) {
                            found = false;
                            break;
                        }
                    }

                if (found)
                    return i - partLen + 1;
            }

        return -1;
    }
}

Related

  1. lastIndexOfIgnoreCase(String str, String searchStr, int startPos)
  2. lastIndexOfImpl(final String str, final String search, final int fromIndex, final boolean ignoreCase)
  3. lastIndexOfLetter(String string)
  4. lastIndexOfNewline(CharSequence theChars, int aStart)
  5. lastIndexOfNonWhitespace(final String src)
  6. lastIndexOfPathSeparator(String str)
  7. lastIndexOfSeparator(String path)
  8. lastIndexOfUCL(String value)
  9. lastIndexOfUnnested(String str, char chr, String openBalance, String closeBalance)