Java String Index Of indexOfIgnoreCase(String str, String substring)

Here you can find the source of indexOfIgnoreCase(String str, String substring)

Description

Helper method to obtain the starting index of a substring within another string, ignoring their case.

License

Open Source License

Parameter

Parameter Description
str the string in which to search for the <tt>substring</tt> argument
substring the substring to search for in <tt>str</tt>

Return

if the substring argument occurs as a substring within str, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned

Declaration

public static int indexOfIgnoreCase(String str, String substring) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.  j  av a2s  .  co  m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    /**
     * Helper method to obtain the starting index of a substring within another
     * string, ignoring their case.  This method is expensive because it has
     * to set each character of each string to lower case before doing the
     * comparison.  Uses the default <code>Locale</code> for case conversion.
     *
     * @param str the string in which to search for the <tt>substring</tt>
     *  argument
     * @param substring the substring to search for in <tt>str</tt>
     * @return if the <tt>substring</tt> argument occurs as a substring within
     *  <tt>str</tt>, then the index of the first character of the first such
     *  substring is returned; if it does not occur as a substring, -1 is
     *  returned
     */
    public static int indexOfIgnoreCase(String str, String substring) {
        return indexOfIgnoreCase(str, substring, Locale.getDefault());
    }

    /**
     * Helper method to obtain the starting index of a substring within another
     * string, ignoring their case.  This method is expensive because it has
     * to set each character of each string to lower case before doing the
     * comparison.
     *
     * @param str the string in which to search for the <tt>substring</tt>
     *  argument
     * @param substring the substring to search for in <tt>str</tt>
     * @param locale the <code>Locale</code> to use when converting the
     *  case of <code>str</code> and <code>substring</code>.  This is necessary because
     *  case conversion is <code>Locale</code> specific.
     * @return if the <tt>substring</tt> argument occurs as a substring within
     *  <tt>str</tt>, then the index of the first character of the first such
     *  substring is returned; if it does not occur as a substring, -1 is
     *  returned
     */
    public static int indexOfIgnoreCase(String str, String substring, Locale locale) {
        // Look for the index after the expensive conversion to lower case.
        return str.toLowerCase(locale).indexOf(substring.toLowerCase(locale));
    }
}

Related

  1. getTabIndexes(String text)
  2. getUserGroupsFromImpex( final String impexContent, final int uidIndex)
  3. indexOf(final String str, final String searchString)
  4. indexOfClosingBracket(String text, int openingBracket)
  5. indexOfIgnoreCase(final String src, char c, int startIndex, int endIndex)
  6. indexOfMultiple(String line, char character, int count, int startIndex)
  7. indexOfWord(String word, String string)
  8. isPhaseGroupContainsPhase(int index, String phaseName)
  9. lastIndexOf(String pattern, String s)