Refactor StringUtils#startsWithAny()

- Refactor to use Strings
- Gains a case-insensitive implementation
This commit is contained in:
Gary Gregory 2024-09-26 11:13:34 -04:00
parent 6e78b38f05
commit 479532b83d
2 changed files with 42 additions and 11 deletions

View File

@ -7774,7 +7774,7 @@ public class StringUtils {
} }
/** /**
* Check if a CharSequence starts with a specified prefix. * Tests if a CharSequence starts with a specified prefix.
* *
* <p>{@code null}s are handled without exceptions. Two {@code null} * <p>{@code null}s are handled without exceptions. Two {@code null}
* references are considered to be equal. The comparison is case-sensitive.</p> * references are considered to be equal. The comparison is case-sensitive.</p>
@ -7802,7 +7802,7 @@ public class StringUtils {
} }
/** /**
* Check if a CharSequence starts with any of the provided case-sensitive prefixes. * Tests if a CharSequence starts with any of the provided case-sensitive prefixes.
* *
* <pre> * <pre>
* StringUtils.startsWithAny(null, null) = false * StringUtils.startsWithAny(null, null) = false
@ -7822,17 +7822,11 @@ public class StringUtils {
* the input {@code sequence} begins with any of the provided case-sensitive {@code searchStrings}. * the input {@code sequence} begins with any of the provided case-sensitive {@code searchStrings}.
* @since 2.5 * @since 2.5
* @since 3.0 Changed signature from startsWithAny(String, String[]) to startsWithAny(CharSequence, CharSequence...) * @since 3.0 Changed signature from startsWithAny(String, String[]) to startsWithAny(CharSequence, CharSequence...)
* @deprecated Use {@link Strings#startsWithAny(CharSequence, CharSequence...) Strings.CI.startsWithAny(CharSequence, CharSequence...)}
*/ */
@Deprecated
public static boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) { public static boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
if (isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) { return Strings.CS.startsWithAny(sequence, searchStrings);
return false;
}
for (final CharSequence searchString : searchStrings) {
if (Strings.CS.startsWith(sequence, searchString)) {
return true;
}
}
return false;
} }
/** /**

View File

@ -1410,4 +1410,41 @@ public abstract class Strings {
} }
return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, preLen); return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, preLen);
} }
/**
* Tests if a CharSequence starts with any of the provided case-sensitive prefixes.
*
* <p>
* Case-sensitive examples
* </p>
*
* <pre>
* StringUtils.startsWithAny(null, null) = false
* StringUtils.startsWithAny(null, new String[] {"abc"}) = false
* StringUtils.startsWithAny("abcxyz", null) = false
* StringUtils.startsWithAny("abcxyz", new String[] {""}) = true
* StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true
* StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
* StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX") = false
* StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc") = false
* </pre>
*
* @param sequence the CharSequence to check, may be null
* @param searchStrings the case-sensitive CharSequence prefixes, may be empty or contain {@code null}
* @see StringUtils#startsWith(CharSequence, CharSequence)
* @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or the input {@code sequence} begins with
* any of the provided case-sensitive {@code searchStrings}.
*/
public boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
if (StringUtils.isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) {
return false;
}
for (final CharSequence searchString : searchStrings) {
if (startsWith(sequence, searchString)) {
return true;
}
}
return false;
}
} }