Java String Ends With endsWithIgnoreCase(final String text, final String suffix)

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

Description

Tests if the string ends with the specified suffix.

License

Open Source License

Parameter

Parameter Description
text the string to test.
suffix the suffix.

Return

true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the suffix is the empty string or is equal to this String object as determined by the method. If the text or suffix argument is null false is returned.

Declaration

public static boolean endsWithIgnoreCase(final String text, final String suffix) 

Method Source Code

//package com.java2s;
/*//from ww w.  ja v  a2s.  c  o m
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

public class Main {
    /**
     * Tests if the string ends with the specified suffix.
     * 
     * @param text the string to test.
     * @param suffix the suffix.
     * @return <code>true</code> if the character sequence represented by the argument is a suffix of the character sequence
     *         represented by this object; <code>false</code> otherwise. Note that the result will be <code>true</code> if the
     *         suffix is the empty string or is equal to this <code>String</code> object as determined by the
     *         {@link #equals(Object)} method. If the text or suffix argument is null <code>false</code> is returned.
     */
    public static boolean endsWithIgnoreCase(final String text, final String suffix) {
        if (isEmpty(text)) {
            return false;
        }
        if (suffix == null) {
            return false;
        }
        int textLength = text.length();
        int suffixLength = suffix.length();
        if (suffixLength == 0) {
            return true;
        }
        if (suffixLength > textLength) {
            return false;
        }
        int offset = textLength - suffixLength;
        char[] chArray = suffix.toCharArray();
        for (int i = 0; i != chArray.length; ++i) {
            char ch1 = chArray[i];
            char ch2 = text.charAt(offset + i);
            if (ch1 == ch2 || Character.toLowerCase(ch1) == Character.toLowerCase(ch2)) {
                // continue
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>
     * Returns whether the specified text is either empty or null.
     * </p>
     * 
     * @param text The text to check; may be null;
     * @return True if the specified text is either empty or null.
     * @since 4.0
     */
    public static boolean isEmpty(final String text) {
        return (text == null || text.length() == 0);
    }

    public static String toLowerCase(String str) {
        String newStr = convertBasicLatinToLower(str);
        if (newStr == null) {
            return str.toLowerCase();
        }
        return newStr;
    }

    private static String convertBasicLatinToLower(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (isBasicLatinUpperCase(chars[i])) {
                chars[i] = (char) ('a' + (chars[i] - 'A'));
            } else if (!isBasicLatinChar(chars[i])) {
                return null;
            }
        }
        return new String(chars);
    }

    private static boolean isBasicLatinUpperCase(char c) {
        return c >= 'A' && c <= 'Z';
    }

    private static boolean isBasicLatinChar(char c) {
        return c <= '\u007F';
    }
}

Related

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