Class StringExpressionVariable
- java.lang.Object
-
- com.broadleafcommerce.rulesengine.expression.util.StringExpressionVariable
-
- All Implemented Interfaces:
ExpressionVariable
public class StringExpressionVariable extends Object implements 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 aroundStringUtils
to provide a layer of abstraction and because StringUtils employs static methods. Note that not all methods ofStringUtils
have been implemented just those that seemed most relevant for expression evaluation.- Author:
- Nathan Moore (nathanmoore).
-
-
Constructor Summary
Constructors Constructor Description StringExpressionVariable()
Setslocale
toLocale.ENGLISH
StringExpressionVariable(Locale locale)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
capitalize(String str)
Null-safe convert to title caseString
chomp(String str)
Removes one newline from end of the string if presentString
chop(String str)
Remove the last character from a stringint
compare(String str1, String str2)
Null-safe string compare.int
compareIgnoreCase(String str1, String str2)
Null-safe string compare.int
compareIgnoreCaseNullIsMore(String str1, String str2)
Null-safe string compare.int
compareNullIsMore(String str1, String str2)
Null-safe string compare.boolean
contains(CharSequence seq, CharSequence searchChar)
Whether the string contains the substringboolean
containsIgnoreCase(CharSequence seq, CharSequence searchChar)
Whether the string contains the substring.boolean
containsWhitespace(CharSequence seq)
Whether the string contains whitespace.String
deleteWhitespace(String str)
Deletes all whitespaces from a String as defined byCharacter.isWhitespace(char)
.boolean
endsWith(CharSequence cs, CharSequence prefix)
Whether the text ends with the prefix.boolean
endsWithIgnoreCase(CharSequence cs, CharSequence prefix)
Whether the text ends with the prefix.boolean
equals(CharSequence cs1, CharSequence cs2)
Null-safe string equals.boolean
equalsIgnoreCase(CharSequence cs1, CharSequence cs2)
Null-safe string equals.String
getName()
int
indexOf(CharSequence seq, CharSequence searchSeq)
Returns the index of the given substring with the string starting at the given position.int
indexOfIgnoreCase(CharSequence seq, CharSequence searchSeq)
Returns the index of the given substring with the string starting at the given position.boolean
isBlank(CharSequence cs)
Whether the string is null, empty (""), or blank (" ")boolean
isEmpty(CharSequence cs)
Whether the string is null or empty ("")boolean
isNotBlank(CharSequence cs)
Whether the string is not null, empty(""),or blank (" ")boolean
isNotEmpty(CharSequence cs)
Whether the string is not null or empty ("")<T> String
join(T... elements)
Joins the elements of the provided array into a single String containing the provided of elementsString
joinWith(String separator, Iterable<?> iterable)
Joins the elements of the provided iterable into a single String containing the provided of elements, placing the given separator between elements<T> String
joinWith(String separator, T... elements)
Joins the elements of the provided array into a single String containing the provided of elements, placing the given separator between elementsint
lastIndexOf(CharSequence seq, CharSequence searchChar)
Returns the index of the last occurrence of the given substring.int
lastIndexOfIgnoreCase(CharSequence seq, CharSequence searchChar)
Returns the index of the last occurrence of the given substring.String
leftMost(String str, int len)
Gets a substring from the left-most characters.int
length(CharSequence cs)
Null-safe get string lengthboolean
notEquals(String str1, String str2)
Return true if strings are not equal, else false.boolean
notEqualsIgnoreCase(String str1, String str2)
Return true if strings are not equal, ignoring case, else falseString
remove(String str, String removeStr)
Removes all occurrences of a substring from within the source string.String
removeIgnoreCase(String str, String removeStr)
Removes all occurrences of a substring from within the source string.String
replace(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
.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.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
.String
reverse(String str)
Null-safe string reverseString
rightMost(String str, int len)
Gets a substring from the right-most characters.String[]
split(String str, String separator)
Splits the provided string into an array by the separator string specified.List<String>
splitToList(String str, String separator)
Splits the provided string into a list instead of an array.boolean
startsWith(CharSequence cs, CharSequence prefix)
Whether the text starts with the prefixboolean
startsWithIgnoreCase(CharSequence cs, CharSequence prefix)
Whether the text starts with the prefix.String
strip(String str)
Strips whitespace from the start and end of a String.String
stripChars(String str, String stripChars)
Strips the specified characters from the beginning and end of the string.String
stripToEmpty(String str)
Returns an empty string ("") if the string is null or empty after strippingString
stripToNull(String str)
Returns null if the stripped string is empty.String
substring(String str, int start, int end)
Gets a substring defined by start and end indices from the specified string safely.String
toLower(String str)
Null-safe convert to lowercaseString
toString(Object target)
Converts the target to a string.String
toUpper(String str)
Null-safe convert to uppercaseString
trim(String str)
Removes control characters (char <= 32) from both ends of this String, handlingnull
by returningnull
.String
trimToEmpty(String str)
Returns an empty string ("") if the string is null or empty after trimming.String
trimToNull(String str)
Returns null if the string is empty after trimmingString
truncate(String str, int offset, int maxWidth)
Creates a substring using the specified left-offset and max width.String
unwrap(String str, String wrapToken)
Unwraps a given string from anther string.String
wrap(String str, String wrapWith)
Wraps a string with a string.String
wrapIfMissing(String str, String wrapWith)
Wraps a string with a string if not already present.
-
-
-
Field Detail
-
NAME
public static final String NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
StringExpressionVariable
public StringExpressionVariable()
Setslocale
toLocale.ENGLISH
-
StringExpressionVariable
public StringExpressionVariable(Locale locale)
-
-
Method Detail
-
getName
public final String getName()
- Specified by:
getName
in interfaceExpressionVariable
- Returns:
- Name of the variable, must not be null
-
toString
@Nullable public final String toString(@Nullable Object target)
Converts the target to a string.- Parameters:
target
- object to convert- Returns:
- string representation of object
-
isEmpty
public final boolean isEmpty(@Nullable CharSequence cs)
Whether the string is null or empty ("")
-
isNotEmpty
public final boolean isNotEmpty(@Nullable CharSequence cs)
Whether the string is not null or empty ("")
-
isBlank
public final boolean isBlank(@Nullable CharSequence cs)
Whether the string is null, empty (""), or blank (" ")
-
isNotBlank
public final boolean isNotBlank(@Nullable CharSequence cs)
Whether the string is not null, empty(""),or blank (" ")
-
trim
@Nullable public final String trim(@Nullable String str)
Removes control characters (char <= 32) from both ends of this String, handlingnull
by returningnull
.
-
trimToNull
@Nullable public final String trimToNull(@Nullable String str)
Returns null if the string is empty after trimming
-
trimToEmpty
public final String trimToEmpty(@Nullable String str)
Returns an empty string ("") if the string is null or empty after trimming.
-
notEquals
public final boolean notEquals(String str1, String str2)
Return true if strings are not equal, else false.
-
notEqualsIgnoreCase
public final boolean notEqualsIgnoreCase(String str1, String str2)
Return true if strings are not equal, ignoring case, else false
-
truncate
@Nullable public final String truncate(@Nullable String str, int offset, int maxWidth)
Creates a substring using the specified left-offset and max width.
-
strip
@Nullable public final String strip(@Nullable String str)
Strips whitespace from the start and end of a String.
-
stripToNull
@Nullable public final String stripToNull(@Nullable String str)
Returns null if the stripped string is empty.
-
stripToEmpty
public final String stripToEmpty(@Nullable String str)
Returns an empty string ("") if the string is null or empty after stripping
-
stripChars
@Nullable public final String stripChars(@Nullable String str, @Nullable String stripChars)
Strips the specified characters from the beginning and end of the string.
-
equals
public final boolean equals(@Nullable CharSequence cs1, @Nullable CharSequence cs2)
Null-safe string equals.
-
equalsIgnoreCase
public final boolean equalsIgnoreCase(@Nullable CharSequence cs1, @Nullable CharSequence cs2)
Null-safe string equals. Ignores case
-
compare
public final int compare(@Nullable String str1, @Nullable String str2)
Null-safe string compare.
-
compareIgnoreCase
public final int compareIgnoreCase(@Nullable String str1, @Nullable String str2)
Null-safe string compare. Ignores case
-
compareNullIsMore
public final int compareNullIsMore(@Nullable String str1, @Nullable String str2)
Null-safe string compare. Where null is greater than not null.
-
compareIgnoreCaseNullIsMore
public final int compareIgnoreCaseNullIsMore(@Nullable String str1, @Nullable String str2)
Null-safe string compare. Where null is greater than not null. Ignores case
-
indexOf
public final int indexOf(@Nullable CharSequence seq, @Nullable CharSequence searchSeq)
Returns the index of the given substring with the string starting at the given position.
-
indexOfIgnoreCase
public final int indexOfIgnoreCase(@Nullable CharSequence seq, @Nullable CharSequence searchSeq)
Returns the index of the given substring with the string starting at the given position. Ignores case.
-
lastIndexOf
public final int lastIndexOf(@Nullable CharSequence seq, @Nullable CharSequence searchChar)
Returns the index of the last occurrence of the given substring.
-
lastIndexOfIgnoreCase
public final int lastIndexOfIgnoreCase(@Nullable CharSequence seq, @Nullable CharSequence searchChar)
Returns the index of the last occurrence of the given substring. Ignores case.
-
contains
public final boolean contains(@Nullable CharSequence seq, @Nullable CharSequence searchChar)
Whether the string contains the substring
-
containsIgnoreCase
public final boolean containsIgnoreCase(@Nullable CharSequence seq, @Nullable CharSequence searchChar)
Whether the string contains the substring. Ignores case.
-
containsWhitespace
public final boolean containsWhitespace(@Nullable CharSequence seq)
Whether the string contains whitespace.
-
substring
@Nullable public final String substring(@Nullable String str, int start, int end)
Gets a substring defined by start and end indices from the specified string safely.
-
leftMost
@Nullable public final String leftMost(@Nullable String str, int len)
Gets a substring from the left-most characters.
-
rightMost
@Nullable public final String rightMost(@Nullable String str, int len)
Gets a substring from the right-most characters.
-
split
@Nullable public final String[] split(@Nullable String str, @Nullable String separator)
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
public final List<String> splitToList(@Nullable String str, @Nullable String separator)
Splits the provided string into a list instead of an array.
-
join
@SafeVarargs @Nullable public final <T> String join(@Nullable T... elements)
Joins the elements of the provided array into a single String containing the provided of elements
-
joinWith
@SafeVarargs public final <T> String joinWith(@Nullable String separator, T... elements)
Joins the elements of the provided array into a single String containing the provided of elements, placing the given separator between elements
-
joinWith
@Nullable public final String joinWith(@Nullable String separator, @Nullable Iterable<?> iterable)
Joins the elements of the provided iterable into a single String containing the provided of elements, placing the given separator between elements
-
deleteWhitespace
@Nullable public final String deleteWhitespace(@Nullable String str)
Deletes all whitespaces from a String as defined byCharacter.isWhitespace(char)
.
-
remove
@Nullable public final String remove(@Nullable String str, @Nullable String removeStr)
Removes all occurrences of a substring from within the source string.
-
removeIgnoreCase
@Nullable public final String removeIgnoreCase(@Nullable String str, @Nullable String removeStr)
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
public final String chomp(@Nullable String str)
Removes one newline from end of the string if present
-
chop
@Nullable public final String chop(@Nullable String str)
Remove the last character from a string
-
length
public final int length(@Nullable CharSequence cs)
Null-safe get string length
-
capitalize
@Nullable public final String capitalize(@Nullable String str)
Null-safe convert to title case
-
startsWith
public final boolean startsWith(@Nullable CharSequence cs, @Nullable CharSequence prefix)
Whether the text starts with the prefix
-
startsWithIgnoreCase
public final boolean startsWithIgnoreCase(@Nullable CharSequence cs, @Nullable CharSequence prefix)
Whether the text starts with the prefix. Ignores case.
-
endsWith
public final boolean endsWith(@Nullable CharSequence cs, @Nullable CharSequence prefix)
Whether the text ends with the prefix.
-
endsWithIgnoreCase
public final boolean endsWithIgnoreCase(@Nullable CharSequence cs, @Nullable CharSequence prefix)
Whether the text ends with the prefix. Ignores case.
-
wrap
@Nullable public final String wrap(@Nullable String str, @Nullable String wrapWith)
Wraps a string with a string.
-
wrapIfMissing
@Nullable public final String wrapIfMissing(@Nullable String str, @Nullable String wrapWith)
Wraps a string with a string if not already present.
-
-