Test if the given String ends with the specified suffix, ignoring upper/lower case. - Java java.lang

Java examples for java.lang:String End

Description

Test if the given String ends with the specified suffix, ignoring upper/lower case.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        String suffix = "java2s.com";
        System.out.println(endsWithIgnoreCase(str, suffix));
    }//from   w  w  w .  j  a va 2s  . com

    /**
     * Test if the given String ends with the specified suffix, ignoring upper/lower case.
     *
     * @param str
     *            the String to check
     * @param suffix
     *            the suffix to look for
     * @see java.lang.String#endsWith
     */
    public static boolean endsWithIgnoreCase(final String str,
            final String suffix) {

        if ((str == null) || (suffix == null))
            return false;
        if (str.endsWith(suffix))
            return true;
        if (str.length() < suffix.length())
            return false;

        final String lcStr = str.substring(str.length() - suffix.length())
                .toLowerCase();
        final String lcSuffix = suffix.toLowerCase();
        return lcStr.equals(lcSuffix);
    }
}

Related Tutorials