Java String Ends With endsWithIgnoreCase(String str, String suffix)

Here you can find the source of endsWithIgnoreCase(String str, String suffix)

Description

This method is equivalent to String#endsWith(String) with the exception that String#equalsIgnoreCase(String) is used, instead of String#equals(Object) .

License

Open Source License

Parameter

Parameter Description
str the String to test
suffix the suffix

Return

true if the passes string ends with the passed suffix, ignoring cases

Declaration

public static boolean endsWithIgnoreCase(String str, String suffix) 

Method Source Code

//package com.java2s;
/*/*www. j a v a 2s .  c o  m*/
 * Copyright (c) 2012, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html
 * 
 * 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 {
    /**
     * This method is equivalent to {@link String#endsWith(String)} with the exception that
     * {@link String#equalsIgnoreCase(String)} is used, instead of {@link String#equals(Object)}.
     * 
     * @see String#endsWith(String)
     * @param str the String to test
     * @param suffix the suffix
     * @return <code>true</code> if the passes string ends with the passed suffix, ignoring cases
     */
    public static boolean endsWithIgnoreCase(String str, String suffix) {
        // if one of the two inputs is null, return false.
        if (str == null || suffix == null) {
            return false;
        }
        int strLength = str.length();
        int suffixLength = suffix.length();
        // cover the trivial case where the suffix has no length
        if (suffixLength == 0) {
            return true;
        }
        // if the string is shorter than the suffix, return false
        if (strLength < suffixLength) {
            return false;
        }
        String strSuffix = str.substring(strLength - suffixLength);
        return strSuffix.equalsIgnoreCase(suffix);
    }
}

Related

  1. endsWithIgnoreCase(String str, String suffix)
  2. endsWithIgnoreCase(String str, String suffix)
  3. endsWithIgnoreCase(String str, String suffix)
  4. endsWithIgnoreCase(String str, String suffix)
  5. endsWithIgnoreCase(String str, String suffix)
  6. endsWithIgnoreCase(String string, String suffix)
  7. endsWithIgnoreCase(String target1, String target2)
  8. endsWithIgnoreCase(String text, String suffix)
  9. endsWithIgnoreCase(String toCheck, String suffix)