Class 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 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).
    • Constructor Detail

      • StringExpressionVariable

        public StringExpressionVariable​(Locale locale)
    • Method Detail

      • 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, handling null by returning null.
      • 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 split
        separator - 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
      • 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 to timesToReplace.
        Parameters:
        text - text to search and replace in
        replaceTxt - text to replace
        replacement - 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 to timesToReplace. Ignores case.
        Parameters:
        text - text to search and replace in
        replaceTxt - text to replace
        replacement - 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
      • toUpper

        @Nullable
        public final String toUpper​(@Nullable
                                    String str)
        Null-safe convert to uppercase
      • toLower

        @Nullable
        public final String toLower​(@Nullable
                                    String str)
        Null-safe convert to lowercase
      • capitalize

        @Nullable
        public final String capitalize​(@Nullable
                                       String str)
        Null-safe convert to title case
      • reverse

        @Nullable
        public final String reverse​(@Nullable
                                    String str)
        Null-safe string reverse
      • 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.
      • unwrap

        @Nullable
        public final String unwrap​(@Nullable
                                   String str,
                                   @Nullable
                                   String wrapToken)
        Unwraps a given string from anther string.