Class Strings


  • public final class Strings
    extends java.lang.Object
    Strings Utilities.
    Since:
    1.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int convertToInt​(java.lang.String text, int fallback)
      Convert string to int.
      static java.lang.String convertToString​(java.io.InputStream inputStream)
      Convert to string.
      static java.lang.String duplicate​(java.lang.String value, int times)
      Construct from given value in multiple times.
      static java.lang.String fallback​(java.lang.String value, @NotNull java.util.function.Supplier<java.lang.String> fallback)
      Fallback string.
      static java.lang.String fallback​(java.lang.String value, java.lang.String fallback)
      Checks given string is blank then fallback to given value
      static java.lang.String format​(java.lang.String msgPattern, java.lang.Object... params)
      Format string.
      static java.lang.Object getFirstNotNull​(java.lang.Object... objects)
      Gets first not null.
      static java.lang.String getFirstNotNull​(java.lang.String... strings)
      Gets first not null.
      static java.lang.String getMatchValue​(java.lang.String with, java.lang.String[] withMatch)
      Gets match value.
      static java.lang.String getMatchValueOrFirstOne​(java.lang.String with, java.lang.String[] withMatch)
      Gets match value or first one.
      static boolean in​(java.lang.String with, boolean equalsIgnoreCase, java.lang.String... values)
      In boolean.
      static boolean in​(java.lang.String with, java.lang.String... values)
      In boolean.
      static boolean isBlank​(java.lang.String text)
      Check given text is blank or not.
      static boolean isNotBlank​(java.lang.String text)
      Check given text is not blank or not.
      static java.lang.String optimizeMultipleSpace​(java.lang.String text)
      Checks that the specified string reference is not blank then remove multiple space characters to one space.
      static java.lang.String optimizeNoSpace​(java.lang.String text)
      Checks that the specified string reference is not blank then remove all space characters.
      static java.lang.String padLeft​(java.lang.String inputString, int length)
      Pad left string with blank ' '
      static java.lang.String padLeftZeros​(java.lang.String inputString, int length)
      Pad left string with '0'
      static java.lang.String requiredMinLength​(java.lang.String text, int minLength)
      Checks that the specified string reference is not blank and its length greater than given input.
      static java.lang.String requireNotBlank​(java.lang.Object object, java.lang.String message)
      Checks that the specified object reference is not blank.
      static java.lang.String requireNotBlank​(java.lang.String text)
      Checks that the specified string reference is not blank.
      static java.lang.String requireNotBlank​(java.lang.String text, @NotNull java.util.function.Supplier<? extends java.lang.RuntimeException> errorSupplier)
      Checks that the specified string reference is not blank.
      static java.lang.String requireNotBlank​(java.lang.String text, java.lang.String message)
      Checks that the specified string reference is not blank.
      static <T> T requireNotBlank​(T obj, @NotNull java.util.function.Supplier<? extends java.lang.RuntimeException> errorSupplier)
      Checks that the specified object reference is not null or not blank if given object is string .
      static java.lang.String toSnakeLowerCase​(@NotNull java.lang.String text)
      To snake case lc string.
      static java.lang.String toSnakeUpperCase​(@NotNull java.lang.String text)
      To snake case uc string.
      static java.lang.String toString​(java.lang.Object object)
      To String.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toString

        public static java.lang.String toString​(java.lang.Object object)
        To String.
        Parameters:
        object - the object cast to string.
        Returns:
        blank if null, else Object.toString() with trim
        Since:
        1.0.0
      • duplicate

        public static java.lang.String duplicate​(java.lang.String value,
                                                 int times)
        Construct from given value in multiple times.
        Parameters:
        value - Value to duplicate
        times - Times to duplicate
        Returns:
        Append value
        Since:
        1.0.0
      • isBlank

        public static boolean isBlank​(java.lang.String text)
        Check given text is blank or not.
        Parameters:
        text - the text to check for blank
        Returns:
        True if blank, else otherwise
        Since:
        1.0.0
      • isNotBlank

        public static boolean isNotBlank​(java.lang.String text)
        Check given text is not blank or not. The reversion of isBlank(String)
        Parameters:
        text - the text to check for blank
        Returns:
        True if not blank, else otherwise
        Since:
        1.0.0
      • requireNotBlank

        public static java.lang.String requireNotBlank​(java.lang.String text)
        Checks that the specified string reference is not blank. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(Bar bar) {
             this.bar = Strings.requireNotBlank(bar);
         }
         
        Parameters:
        text - the text to check for blank
        Returns:
        Trimmed text if not blank
        Throws:
        java.lang.IllegalArgumentException - if obj is blank
        Since:
        1.0.0
      • requireNotBlank

        public static java.lang.String requireNotBlank​(java.lang.String text,
                                                       java.lang.String message)
        Checks that the specified string reference is not blank. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(Bar bar) {
             this.bar = Strings.requireNotBlank(bar, "String cannot blank");
         }
         
        Parameters:
        text - the text to check for blank
        message - the error message will be included in exception
        Returns:
        Trimmed text if not blank
        Throws:
        java.lang.IllegalArgumentException - if obj is blank
        Since:
        1.0.0
      • requireNotBlank

        public static java.lang.String requireNotBlank​(java.lang.String text,
                                                       @NotNull
                                                       @NotNull java.util.function.Supplier<? extends java.lang.RuntimeException> errorSupplier)
        Checks that the specified string reference is not blank. If blank, raise exception in given supplier.
        Parameters:
        text - the text
        errorSupplier - the exception supplier
        Returns:
        Trimmed text if not blank
        Since:
        1.0.0
      • requireNotBlank

        public static java.lang.String requireNotBlank​(java.lang.Object object,
                                                       java.lang.String message)
        Checks that the specified object reference is not blank. if not blank, it will convert to string by toString() method
        Parameters:
        object - the object to check
        message - the error message will be included in exception
        Returns:
        Trimmed object if not blank
        Throws:
        java.lang.IllegalArgumentException - if obj is blank
        Since:
        1.0.0
      • requireNotBlank

        public static <T> T requireNotBlank​(T obj,
                                            @NotNull
                                            @NotNull java.util.function.Supplier<? extends java.lang.RuntimeException> errorSupplier)
        Checks that the specified object reference is not null or not blank if given object is string . If blank, raise exception in given supplier.
        Type Parameters:
        T - Type of object
        Parameters:
        obj - the object
        errorSupplier - the error supplier
        Returns:
        the t
        Since:
        1.0.0
      • optimizeMultipleSpace

        public static java.lang.String optimizeMultipleSpace​(java.lang.String text)
        Checks that the specified string reference is not blank then remove multiple space characters to one space.
        Parameters:
        text - Given input
        Returns:
        Optimization text
        Throws:
        java.lang.IllegalArgumentException - if text is blank
        Since:
        1.0.0
      • optimizeNoSpace

        public static java.lang.String optimizeNoSpace​(java.lang.String text)
        Checks that the specified string reference is not blank then remove all space characters.
        Parameters:
        text - Given input
        Returns:
        Optimization text
        Throws:
        java.lang.IllegalArgumentException - if text is blank
        Since:
        1.0.0
      • requiredMinLength

        public static java.lang.String requiredMinLength​(java.lang.String text,
                                                         int minLength)
        Checks that the specified string reference is not blank and its length greater than given input.
        Parameters:
        text - Given input
        minLength - Min length
        Returns:
        this text if conforms condition
        Throws:
        java.lang.IllegalArgumentException - if text or optimized text is blank
        Since:
        1.0.0
      • toSnakeLowerCase

        public static java.lang.String toSnakeLowerCase​(@NotNull
                                                        @NotNull java.lang.String text)
        To snake case lc string.
        Parameters:
        text - the text
        Returns:
        the string
        Since:
        1.0.0
      • toSnakeUpperCase

        public static java.lang.String toSnakeUpperCase​(@NotNull
                                                        @NotNull java.lang.String text)
        To snake case uc string.
        Parameters:
        text - the text
        Returns:
        the string
        Since:
        1.0.0
      • convertToInt

        public static int convertToInt​(java.lang.String text,
                                       int fallback)
        Convert string to int.
        Parameters:
        text - Text to convert
        fallback - Default value to fallback
        Returns:
        Int value that corresponding to given text
        Since:
        1.0.0
      • convertToString

        public static java.lang.String convertToString​(java.io.InputStream inputStream)
        Convert to string.
        Parameters:
        inputStream - the input stream
        Returns:
        the string
        Since:
        1.0.0
      • format

        public static java.lang.String format​(java.lang.String msgPattern,
                                              java.lang.Object... params)
        Format string.
        Parameters:
        msgPattern - the msg pattern
        params - the params
        Returns:
        the string
        Since:
        1.0.0
      • fallback

        public static java.lang.String fallback​(java.lang.String value,
                                                java.lang.String fallback)
        Checks given string is blank then fallback to given value
        Parameters:
        value - String
        fallback - Fallback value, must not blank
        Returns:
        given value if not blank, otherwise fallback
        Since:
        1.0.0
      • fallback

        public static java.lang.String fallback​(java.lang.String value,
                                                @NotNull
                                                @NotNull java.util.function.Supplier<java.lang.String> fallback)
        Fallback string.
        Parameters:
        value - the value
        fallback - the fallback
        Returns:
        the string
        Since:
        1.0.0
      • in

        public static boolean in​(java.lang.String with,
                                 boolean equalsIgnoreCase,
                                 java.lang.String... values)
        In boolean.
        Parameters:
        with - the with
        equalsIgnoreCase - the equals ignore case
        values - the values
        Returns:
        the boolean
        Since:
        1.0.0
      • in

        public static boolean in​(java.lang.String with,
                                 java.lang.String... values)
        In boolean.
        Parameters:
        with - the with
        values - the values
        Returns:
        the boolean
        Since:
        1.0.0
      • getFirstNotNull

        public static java.lang.String getFirstNotNull​(java.lang.String... strings)
        Gets first not null.
        Parameters:
        strings - the strings
        Returns:
        the first not null
        Since:
        1.0.0
      • getFirstNotNull

        public static java.lang.Object getFirstNotNull​(java.lang.Object... objects)
        Gets first not null.
        Parameters:
        objects - the objects
        Returns:
        the first not null
        Since:
        1.0.0
      • getMatchValueOrFirstOne

        public static java.lang.String getMatchValueOrFirstOne​(java.lang.String with,
                                                               java.lang.String[] withMatch)
        Gets match value or first one.
        Parameters:
        with - the with
        withMatch - the with match
        Returns:
        the match value or first one
        Since:
        1.0.0
      • getMatchValue

        public static java.lang.String getMatchValue​(java.lang.String with,
                                                     java.lang.String[] withMatch)
        Gets match value.
        Parameters:
        with - the with
        withMatch - the with match
        Returns:
        the match value
        Since:
        1.0.0
      • padLeft

        public static java.lang.String padLeft​(java.lang.String inputString,
                                               int length)
        Pad left string with blank ' '
        Parameters:
        inputString - input string
        length - total length
        Returns:
        padded string
      • padLeftZeros

        public static java.lang.String padLeftZeros​(java.lang.String inputString,
                                                    int length)
        Pad left string with '0'
        Parameters:
        inputString - input string
        length - total length
        Returns:
        padded string