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

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

Description

Check if a String ends with a specified suffix.

nulls are handled without exceptions.

License

Open Source License

Parameter

Parameter Description
str the String to check, may be null
suffix the suffix to find, may be null

Return

true if the String ends with the suffix, case sensitive, or both null

Declaration

public static boolean endsWith(String str, String suffix) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from ww w .j  a v  a 2  s .co  m*/
     * <p>Check if a String ends with a specified suffix.</p>
     * <p/>
     * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
     * references are considered to be equal. The comparison is case sensitive.</p>
     * <p/>
     * <pre>
     * StrUtils.endsWith(null, null)      = true
     * StrUtils.endsWith(null, "abcdef")  = false
     * StrUtils.endsWith("def", null)     = false
     * StrUtils.endsWith("def", "abcdef") = true
     * StrUtils.endsWith("def", "ABCDEF") = false
     * </pre>
     *
     * @param str    the String to check, may be null
     * @param suffix the suffix to find, may be null
     * @return <code>true</code> if the String ends with the suffix, case sensitive, or
     * both <code>null</code>
     * @see String#endsWith(String)
     * @since 2.4
     */
    public static boolean endsWith(String str, String suffix) {
        return endsWith(str, suffix, false);
    }

    /**
     * <p>Check if a String ends with a specified suffix (optionally case insensitive).</p>
     *
     * @param str        the String to check, may be null
     * @param suffix     the suffix to find, may be null
     * @param ignoreCase inidicates whether the compare should ignore case
     *                   (case insensitive) or not.
     * @return <code>true</code> if the String starts with the prefix or
     * both <code>null</code>
     * @see String#endsWith(String)
     */
    private static boolean endsWith(String str, String suffix, boolean ignoreCase) {
        if (str == null || suffix == null) {
            return (str == null && suffix == null);
        }
        if (suffix.length() > str.length()) {
            return false;
        }
        int strOffset = str.length() - suffix.length();
        return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
    }

    /**
     * Gets a String's length or <code>0</code> if the String is <code>null</code>.
     *
     * @param str a String or <code>null</code>
     * @return String length or <code>0</code> if the String is <code>null</code>.
     * @since 2.4
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }
}

Related

  1. endsWith(String str, char c)
  2. endsWith(String str, char suffix)
  3. endsWith(String str, char suffix)
  4. endsWith(String str, String end, boolean caseSensitive)
  5. endsWith(String str, String mark)
  6. endsWith(String str, String suffix)
  7. endsWith(String str, String suffix, boolean ignoreCase)
  8. EndsWith(String str, String suffix, int strStartPos)
  9. endsWith(String string, char character)