Here you can find the source of endsWith(String str, String suffix)
Check if a String ends with a specified suffix.
null
s are handled without exceptions.
Parameter | Description |
---|---|
str | the String to check, may be null |
suffix | the suffix to find, may be null |
true
if the String ends with the suffix, case sensitive, or both null
public static boolean endsWith(String str, String suffix)
//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(); } }