Java String Ends With endsWithIgnoreCase(final String str, final String end)

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

Description

Returns true iff str.toLowerCase().endsWith(end.toLowerCase()) implementation is not creating extra strings.

License

Open Source License

Declaration

public static final boolean endsWithIgnoreCase(final String str, final String end) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * 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  a v a2 s.  com
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Returns true iff str.toLowerCase().endsWith(end.toLowerCase())
     * implementation is not creating extra strings.
     */
    public static final boolean endsWithIgnoreCase(final String str, final String end) {

        if (str == null && end == null) {
            return true;
        }
        if (str == null) {
            return false;
        }
        if (end == null) {
            return false;
        }
        if (str.equals(end)) {
            return true;
        }

        final int strLength = str.length();
        final int endLength = end.length();

        // return false if the string is smaller than the end.
        if (endLength > strLength) {
            return false;
        }

        // return false if any character of the end are
        // not the same in lower case.
        for (int i = 1; i <= endLength; i++) {
            if (Character.toLowerCase(end.charAt(endLength - i)) != Character
                    .toLowerCase(str.charAt(strLength - i))) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. endsWithIgnoreCase(final String base, final String end)
  2. endsWithIgnoreCase(final String haystack, final String needle)
  3. endsWithIgnoreCase(final String haystack, final String needle)
  4. endsWithIgnoreCase(final String input, final String suffix)
  5. endsWithIgnoreCase(final String source, final String target)
  6. endsWithIgnoreCase(final String string, final String end)
  7. endsWithIgnoreCase(final String text, final String suffix)
  8. endsWithIgnoreCase(final String text, final String suffix)
  9. endsWithIgnoreCase(final String text, final String suffix)