Class StringExpressionVariable
java.lang.Object
com.broadleafcommerce.rulesengine.expression.util.StringExpressionVariable
- All Implemented Interfaces:
ExpressionVariable
Common, null-safe, string operations
An instance of this class will usually be accessible as a variable within an expression via
#strings
. This is essentially a wrapper around StringUtils
to provide a layer of
abstraction and because StringUtils employs static methods. Note that not all methods of
StringUtils
have been implemented just those that seemed most relevant for expression
evaluation.
- Author:
- Nathan Moore (nathanmoore).
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal String
capitalize
(Object o) Null-safe convert to title casefinal String
Removes one newline from end of the string if presentfinal String
Remove the last character from a stringfinal int
Null-safe string compare.final int
compareIgnoreCase
(Object o, String str2) Null-safe string compare.final int
compareIgnoreCaseNullIsMore
(Object o, String str2) Null-safe string compare.final int
compareNullIsMore
(Object o, String str2) Null-safe string compare.final boolean
contains
(Object o, CharSequence searchChar) Whether the string contains the substringfinal boolean
containsIgnoreCase
(Object o, CharSequence searchChar) Whether the string contains the substring.final boolean
Whether the string contains whitespace.protected String
final String
Deletes all whitespaces from a String as defined byCharacter.isWhitespace(char)
.final boolean
endsWith
(Object o, CharSequence prefix) Whether the text ends with the prefix.final boolean
endsWithIgnoreCase
(Object o, CharSequence prefix) Whether the text ends with the prefix.final boolean
equals
(Object o, CharSequence cs2) Null-safe string equals.final boolean
equalsIgnoreCase
(Object o, CharSequence cs2) Null-safe string equals.final String
getName()
final int
indexOf
(Object o, CharSequence searchSeq) Returns the index of the given substring with the string starting at the given position.final int
indexOfIgnoreCase
(Object o, CharSequence searchSeq) Returns the index of the given substring with the string starting at the given position.final boolean
Whether the string is null, empty (""), or blank (" ")final boolean
Whether the string is null or empty ("")final boolean
isNotBlank
(Object o) Whether the string is not null, empty(""),or blank (" ")final boolean
isNotEmpty
(Object o) Whether the string is not null or empty ("")final <T> String
join
(T... elements) Joins the elements of the provided array into a single String containing the provided of elementsfinal String
Joins the elements of the provided iterable into a single String containing the provided of elements, placing the given separator between elementsfinal <T> String
Joins the elements of the provided array into a single String containing the provided of elements, placing the given separator between elementsfinal int
lastIndexOf
(Object o, CharSequence searchChar) Returns the index of the last occurrence of the given substring.final int
lastIndexOfIgnoreCase
(Object o, CharSequence searchChar) Returns the index of the last occurrence of the given substring.final String
Gets a substring from the left-most characters.final int
Null-safe get string lengthfinal boolean
notContains
(Object o, CharSequence searchChar) The inverse ofcontains(Object, CharSequence)
final boolean
notContainsIgnoreCase
(Object o, CharSequence searchChar) The inverse ofnotContainsIgnoreCase(Object, CharSequence)
.final boolean
Return true if strings are not equal, else false.final boolean
notEqualsIgnoreCase
(Object o, String str2) Return true if strings are not equal, ignoring case, else falsefinal String
Removes all occurrences of a substring from within the source string.final String
removeIgnoreCase
(String str, String removeStr) Removes all occurrences of a substring from within the source string.final String
Replaces each substring of the text String that matches the given regular expression with the given replacement up totimesToReplace
.final String
replaceAll
(String text, String regex, String replacement) Replaces each substring of the text String that matches the given regular expression with the given replacement.final String
replaceIgnoreCase
(String text, String replaceTxt, String replacement) Replaces each substring of the text String that matches the given regular expression with the given replacement up totimesToReplace
.final String
Null-safe string reversefinal String
Gets a substring from the right-most characters.final String[]
Splits the provided string into an array by the separator string specified.splitToList
(Object o, String separator) Splits the provided string into a list instead of an array.final boolean
startsWith
(Object o, CharSequence prefix) Whether the text starts with the prefixfinal boolean
startsWithIgnoreCase
(Object o, CharSequence prefix) Whether the text starts with the prefix.final String
Strips whitespace from the start and end of a String.final String
stripChars
(Object o, String stripChars) Strips the specified characters from the beginning and end of the string.final String
Returns an empty string ("") if the string is null or empty after strippingfinal String
Returns null if the stripped string is empty.final String
Gets a substring defined by start and end indices from the specified string safely.final String
Null-safe convert to lowercasefinal String
Converts the target to a string.final String
Null-safe convert to uppercasefinal String
Removes control characters (char <= 32) from both ends of this String, handlingnull
by returningnull
.final String
Returns an empty string ("") if the string is null or empty after trimming.final String
trimToNull
(Object o) Returns null if the string is empty after trimmingfinal String
Creates a substring using the specified left-offset and max width.final String
Unwraps a given string from anther string.final String
Wraps a string with a string.final String
wrapIfMissing
(Object o, String wrapWith) Wraps a string with a string if not already present.
-
Field Details
-
NAME
- See Also:
-
-
Constructor Details
-
StringExpressionVariable
public StringExpressionVariable()Setslocale
toLocale.ENGLISH
-
StringExpressionVariable
-
-
Method Details
-
getName
- Specified by:
getName
in interfaceExpressionVariable
- Returns:
- Name of the variable, must not be null
-
toString
Converts the target to a string.- Parameters:
target
- object to convert- Returns:
- string representation of object
-
isEmpty
Whether the string is null or empty ("") -
isNotEmpty
Whether the string is not null or empty ("") -
isBlank
Whether the string is null, empty (""), or blank (" ") -
isNotBlank
Whether the string is not null, empty(""),or blank (" ") -
trim
Removes control characters (char <= 32) from both ends of this String, handlingnull
by returningnull
. -
trimToNull
Returns null if the string is empty after trimming -
trimToEmpty
Returns an empty string ("") if the string is null or empty after trimming. -
notEquals
Return true if strings are not equal, else false. -
notEqualsIgnoreCase
Return true if strings are not equal, ignoring case, else false -
truncate
Creates a substring using the specified left-offset and max width. -
strip
Strips whitespace from the start and end of a String. -
stripToNull
Returns null if the stripped string is empty. -
stripToEmpty
Returns an empty string ("") if the string is null or empty after stripping -
stripChars
Strips the specified characters from the beginning and end of the string. -
equals
Null-safe string equals. -
equalsIgnoreCase
Null-safe string equals. Ignores case -
compare
Null-safe string compare. -
compareIgnoreCase
Null-safe string compare. Ignores case -
compareNullIsMore
Null-safe string compare. Where null is greater than not null. -
compareIgnoreCaseNullIsMore
Null-safe string compare. Where null is greater than not null. Ignores case -
indexOf
Returns the index of the given substring with the string starting at the given position. -
indexOfIgnoreCase
Returns the index of the given substring with the string starting at the given position. Ignores case. -
lastIndexOf
Returns the index of the last occurrence of the given substring. -
lastIndexOfIgnoreCase
Returns the index of the last occurrence of the given substring. Ignores case. -
contains
Whether the string contains the substring -
notContains
The inverse ofcontains(Object, CharSequence)
-
containsIgnoreCase
Whether the string contains the substring. Ignores case. -
notContainsIgnoreCase
The inverse ofnotContainsIgnoreCase(Object, CharSequence)
. -
containsWhitespace
Whether the string contains whitespace. -
substring
Gets a substring defined by start and end indices from the specified string safely. -
leftMost
Gets a substring from the left-most characters. -
rightMost
Gets a substring from the right-most characters. -
split
Splits the provided string into an array by the separator string specified.- Parameters:
str
- String to splitseparator
- Separator to separate on- Returns:
- Split string array
-
splitToList
Splits the provided string into a list instead of an array. -
join
Joins the elements of the provided array into a single String containing the provided of elements -
joinWith
Joins the elements of the provided array into a single String containing the provided of elements, placing the given separator between elements -
joinWith
Joins the elements of the provided iterable into a single String containing the provided of elements, placing the given separator between elements -
deleteWhitespace
Deletes all whitespaces from a String as defined byCharacter.isWhitespace(char)
. -
remove
Removes all occurrences of a substring from within the source string. -
removeIgnoreCase
Removes all occurrences of a substring from within the source string. Ignores case -
replaceAll
@Nullable public final String replaceAll(@Nullable String text, @Nullable String regex, @Nullable String replacement) Replaces each substring of the text String that matches the given regular expression with the given replacement.
This method is a
null
safe equivalent to:text.replaceAll(regex, replacement)
Pattern.compile(regex).matcher(text).replaceAll(replacement)
-
replace
@Nullable public final String replace(@Nullable String text, @Nullable String replaceTxt, @Nullable String replacement) Replaces each substring of the text String that matches the given regular expression with the given replacement up totimesToReplace
.- Parameters:
text
- text to search and replace inreplaceTxt
- text to replacereplacement
- what to replace text with- Returns:
- string with replacements
-
replaceIgnoreCase
@Nullable public final String replaceIgnoreCase(@Nullable String text, @Nullable String replaceTxt, @Nullable String replacement) Replaces each substring of the text String that matches the given regular expression with the given replacement up totimesToReplace
. Ignores case.- Parameters:
text
- text to search and replace inreplaceTxt
- text to replacereplacement
- what to replace text with- Returns:
- string with replacements
-
chomp
Removes one newline from end of the string if present -
chop
Remove the last character from a string -
length
Null-safe get string length -
toUpper
Null-safe convert to uppercase -
toLower
Null-safe convert to lowercase -
capitalize
Null-safe convert to title case -
reverse
Null-safe string reverse -
startsWith
Whether the text starts with the prefix -
startsWithIgnoreCase
Whether the text starts with the prefix. Ignores case. -
endsWith
Whether the text ends with the prefix. -
endsWithIgnoreCase
Whether the text ends with the prefix. Ignores case. -
wrap
Wraps a string with a string. -
wrapIfMissing
Wraps a string with a string if not already present. -
unwrap
Unwraps a given string from anther string. -
convertObjectToString
-