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 aroundStringUtilsto provide a layer of abstraction and because StringUtils employs static methods. Note that not all methods ofStringUtilshave been implemented just those that seemed most relevant for expression evaluation.- Author:
- Nathan Moore (nathanmoore).
-
-
Constructor Summary
Constructors Constructor Description StringExpressionVariable()SetslocaletoLocale.ENGLISHStringExpressionVariable(Locale locale)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringcapitalize(String str)Null-safe convert to title caseStringchomp(String str)Removes one newline from end of the string if presentStringchop(String str)Remove the last character from a stringintcompare(String str1, String str2)Null-safe string compare.intcompareIgnoreCase(String str1, String str2)Null-safe string compare.intcompareIgnoreCaseNullIsMore(String str1, String str2)Null-safe string compare.intcompareNullIsMore(String str1, String str2)Null-safe string compare.booleancontains(CharSequence seq, CharSequence searchChar)Whether the string contains the substringbooleancontainsIgnoreCase(CharSequence seq, CharSequence searchChar)Whether the string contains the substring.booleancontainsWhitespace(CharSequence seq)Whether the string contains whitespace.StringdeleteWhitespace(String str)Deletes all whitespaces from a String as defined byCharacter.isWhitespace(char).booleanendsWith(CharSequence cs, CharSequence prefix)Whether the text ends with the prefix.booleanendsWithIgnoreCase(CharSequence cs, CharSequence prefix)Whether the text ends with the prefix.booleanequals(CharSequence cs1, CharSequence cs2)Null-safe string equals.booleanequalsIgnoreCase(CharSequence cs1, CharSequence cs2)Null-safe string equals.StringgetName()intindexOf(CharSequence seq, CharSequence searchSeq)Returns the index of the given substring with the string starting at the given position.intindexOfIgnoreCase(CharSequence seq, CharSequence searchSeq)Returns the index of the given substring with the string starting at the given position.booleanisBlank(CharSequence cs)Whether the string is null, empty (""), or blank (" ")booleanisEmpty(CharSequence cs)Whether the string is null or empty ("")booleanisNotBlank(CharSequence cs)Whether the string is not null, empty(""),or blank (" ")booleanisNotEmpty(CharSequence cs)Whether the string is not null or empty ("")<T> Stringjoin(T... elements)Joins the elements of the provided array into a single String containing the provided of elementsStringjoinWith(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> StringjoinWith(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 elementsintlastIndexOf(CharSequence seq, CharSequence searchChar)Returns the index of the last occurrence of the given substring.intlastIndexOfIgnoreCase(CharSequence seq, CharSequence searchChar)Returns the index of the last occurrence of the given substring.StringleftMost(String str, int len)Gets a substring from the left-most characters.intlength(CharSequence cs)Null-safe get string lengthbooleannotEquals(String str1, String str2)Return true if strings are not equal, else false.booleannotEqualsIgnoreCase(String str1, String str2)Return true if strings are not equal, ignoring case, else falseStringremove(String str, String removeStr)Removes all occurrences of a substring from within the source string.StringremoveIgnoreCase(String str, String removeStr)Removes all occurrences of a substring from within the source string.Stringreplace(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.StringreplaceAll(String text, String regex, String replacement)Replaces each substring of the text String that matches the given regular expression with the given replacement.StringreplaceIgnoreCase(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.Stringreverse(String str)Null-safe string reverseStringrightMost(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.booleanstartsWith(CharSequence cs, CharSequence prefix)Whether the text starts with the prefixbooleanstartsWithIgnoreCase(CharSequence cs, CharSequence prefix)Whether the text starts with the prefix.Stringstrip(String str)Strips whitespace from the start and end of a String.StringstripChars(String str, String stripChars)Strips the specified characters from the beginning and end of the string.StringstripToEmpty(String str)Returns an empty string ("") if the string is null or empty after strippingStringstripToNull(String str)Returns null if the stripped string is empty.Stringsubstring(String str, int start, int end)Gets a substring defined by start and end indices from the specified string safely.StringtoLower(String str)Null-safe convert to lowercaseStringtoString(Object target)Converts the target to a string.StringtoUpper(String str)Null-safe convert to uppercaseStringtrim(String str)Removes control characters (char <= 32) from both ends of this String, handlingnullby returningnull.StringtrimToEmpty(String str)Returns an empty string ("") if the string is null or empty after trimming.StringtrimToNull(String str)Returns null if the string is empty after trimmingStringtruncate(String str, int offset, int maxWidth)Creates a substring using the specified left-offset and max width.Stringunwrap(String str, String wrapToken)Unwraps a given string from anther string.Stringwrap(String str, String wrapWith)Wraps a string with a string.StringwrapIfMissing(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()
SetslocaletoLocale.ENGLISH
-
StringExpressionVariable
public StringExpressionVariable(Locale locale)
-
-
Method Detail
-
getName
public final String getName()
- Specified by:
getNamein 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, handlingnullby 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
nullsafe 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.
-
-