Here you can find the source of startsWith(String str, String prefix, boolean ignoreCase)
Check if a String starts with a specified prefix (optionally case insensitive).
Parameter | Description |
---|---|
str | the String to check, may be null |
prefix | the prefix to find, may be null |
ignoreCase | inidicates whether the compare should ignore case (case insensitive) or not. |
true
if the String starts with the prefix or both null
private static boolean startsWith(String str, String prefix, boolean ignoreCase)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w . j a v a2 s . c om * <p>Check if a String starts with a specified prefix.</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.startsWith(null, null) = true * StrUtils.startsWith(null, "abcdef") = false * StrUtils.startsWith("abc", null) = false * StrUtils.startsWith("abc", "abcdef") = true * StrUtils.startsWith("abc", "ABCDEF") = false * </pre> * * @param str the String to check, may be null * @param prefix the prefix to find, may be null * @return <code>true</code> if the String starts with the prefix, case sensitive, or * both <code>null</code> * @see String#startsWith(String) * @since 2.4 */ public static boolean startsWith(String str, String prefix) { return startsWith(str, prefix, false); } /** * <p>Check if a String starts with a specified prefix (optionally case insensitive).</p> * * @param str the String to check, may be null * @param prefix the prefix 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#startsWith(String) */ private static boolean startsWith(String str, String prefix, boolean ignoreCase) { if (str == null || prefix == null) { return (str == null && prefix == null); } return prefix.length() <= str.length() && str.regionMatches(ignoreCase, 0, prefix, 0, prefix.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(); } }