Class StringUtils
- Author:
- theonefx
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe empty String"".static final intRepresents a failed index search. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequals(CharSequence cs1, CharSequence cs2) Compares two CharSequences, returningtrueif they represent equal sequences of characters.static booleanisBlank(CharSequence cs) Checks if a CharSequence is whitespace, empty ("") or null.static booleanisEmpty(CharSequence cs) Checks if a CharSequence is empty ("") or null.static booleanChecks if a CharSequence is not empty (""), not null and not whitespace only.static booleanChecks if a CharSequence is not empty ("") and not null.static booleanregionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length) Green implementation of regionMatches.static StringsubstringAfter(String str, String separator) Gets the substring after the first occurrence of a separator.static StringsubstringBetween(String str, String tag) Gets the String that is nested in between two instances of the same String.static StringsubstringBetween(String str, String open, String close) Gets the String that is nested in between two Strings.static StringRemoves control characters (char <= 32) from both ends of this String, handlingnullby returningnull.
-
Field Details
-
EMPTY
The empty String"".- Since:
- 2.0
- See Also:
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUNDRepresents a failed index search.- Since:
- 2.1
- See Also:
-
-
Method Details
-
isEmpty
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = falseNOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null- Since:
- 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
-
isNotEmpty
Checks if a CharSequence is not empty ("") and not null.
StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is not empty and not null- Since:
- 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
-
isBlank
Checks if a CharSequence is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is null, empty or whitespace
-
isNotBlank
Checks if a CharSequence is not empty (""), not null and not whitespace only.
Whitespace is defined by
Character.isWhitespace(char).StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is not empty and not null and not whitespace only- Since:
- 2.0, 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
-
trim
Removes control characters (char <= 32) from both ends of this String, handling
nullby returningnull.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32.StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed string,
nullif null String input
-
equals
Compares two CharSequences, returning
trueif they represent equal sequences of characters.nulls are handled without exceptions. Twonullreferences are considered to be equal. The comparison is case sensitive.StringUtils.equals(null, null) = true StringUtils.equals(null, "abc") = false StringUtils.equals("abc", null) = false StringUtils.equals("abc", "abc") = true StringUtils.equals("abc", "ABC") = false- Parameters:
cs1- the first CharSequence, may benullcs2- the second CharSequence, may benull- Returns:
trueif the CharSequences are equal (case-sensitive), or bothnull- See Also:
-
regionMatches
public static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length) Green implementation of regionMatches.- Parameters:
cs- theCharSequenceto be processedignoreCase- whether or not to be case insensitivethisStart- the index to start on thecsCharSequencesubstring- theCharSequenceto be looked forstart- the index to start on thesubstringCharSequencelength- character length of the region- Returns:
- whether the region matched
-
substringAfter
Gets the substring after the first occurrence of a separator. The separator is not returned.
A
nullstring input will returnnull. An empty ("") string input will return the empty string. Anullseparator will return the empty string if the input string is notnull.If nothing is found, the empty string is returned.
StringUtils.substringAfter(null, *) = null StringUtils.substringAfter("", *) = "" StringUtils.substringAfter(*, null) = "" StringUtils.substringAfter("abc", "a") = "bc" StringUtils.substringAfter("abcba", "b") = "cba" StringUtils.substringAfter("abc", "c") = "" StringUtils.substringAfter("abc", "d") = "" StringUtils.substringAfter("abc", "") = "abc"- Parameters:
str- the String to get a substring from, may be nullseparator- the String to search for, may be null- Returns:
- the substring after the first occurrence of the separator,
nullif null String input - Since:
- 2.0
-
substringBetween
Gets the String that is nested in between two instances of the same String.
A
nullinput String returnsnull. Anulltag returnsnull.StringUtils.substringBetween(null, *) = null StringUtils.substringBetween("", "") = "" StringUtils.substringBetween("", "tag") = null StringUtils.substringBetween("tagabctag", null) = null StringUtils.substringBetween("tagabctag", "") = "" StringUtils.substringBetween("tagabctag", "tag") = "abc"- Parameters:
str- the String containing the substring, may be nulltag- the String before and after the substring, may be null- Returns:
- the substring,
nullif no match - Since:
- 2.0
-
substringBetween
Gets the String that is nested in between two Strings. Only the first match is returned.
A
nullinput String returnsnull. Anullopen/close returnsnull(no match). An empty ("") open and close returns an empty string.StringUtils.substringBetween("wx[b]yz", "[", "]") = "b" StringUtils.substringBetween(null, *, *) = null StringUtils.substringBetween(*, null, *) = null StringUtils.substringBetween(*, *, null) = null StringUtils.substringBetween("", "", "") = "" StringUtils.substringBetween("", "", "]") = null StringUtils.substringBetween("", "[", "]") = null StringUtils.substringBetween("yabcz", "", "") = "" StringUtils.substringBetween("yabcz", "y", "z") = "abc" StringUtils.substringBetween("yabczyabcz", "y", "z") = "abc"- Parameters:
str- the String containing the substring, may be nullopen- the String before the substring, may be nullclose- the String after the substring, may be null- Returns:
- the substring,
nullif no match - Since:
- 2.0
-