Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    Some small utilities and constants around handling strings.
    Since:
    2.1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean allCharsMatch​(java.util.function.Predicate<java.lang.Character> p, java.lang.CharSequence s)
      Check to see if all characters in a provided string match to a given Predicate.
      static boolean anyCharsMatch​(java.util.function.Predicate<java.lang.Character> p, java.lang.CharSequence s)
      Check to see if any characters in a provided string match to a given Predicate.
      static java.lang.String emptyToNull​(java.lang.String input)
      Converts an empty string into a null.
      static boolean isNullOrEmpty​(java.lang.String input)
      Check if the provided string is either null or empty.
      static java.lang.String makeRandomString​(int length)
      Produces a random string of the provided length.
      static java.util.Optional<java.lang.String> nonEmptyOptional​(java.lang.String input)
      Simple utility function for returning an Optional that if contents present are guaranteed to not be empty.
      static java.lang.String nullToEmpty​(java.lang.String input)
      Makes sure a given string is not null.
      static java.lang.String padEnd​(java.lang.String sourceStr, int minLength, char padChar)
      Pads the end of the provided string with the provided character until a minimum length is reached.
      static java.lang.String padStart​(java.lang.String sourceStr, int minLength, char padChar)
      Pads the start of the provided string with the provided character until a minimum length is reached.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • allCharsMatch

        public static boolean allCharsMatch​(java.util.function.Predicate<java.lang.Character> p,
                                            java.lang.CharSequence s)
        Check to see if all characters in a provided string match to a given Predicate. This can be easily used when provided predicates from Character. For example providing Character::isLetter for the predicate will verify all characters in the string are letters.
        Parameters:
        p - Character test
        s - CharSequence to check against
        Returns:
        true if predicate returned true for every character in string
        Since:
        5.14
      • anyCharsMatch

        public static boolean anyCharsMatch​(java.util.function.Predicate<java.lang.Character> p,
                                            java.lang.CharSequence s)
        Check to see if any characters in a provided string match to a given Predicate. This can be easily used when provided predicates from Character. For example providing Character::isDigit for the predicate to see if this string contains any numbers.
        Parameters:
        p - Character test
        s - CharSequence to check against
        Returns:
        true if predicate returned true for any characters in the string
        Since:
        5.14
      • nullToEmpty

        public static java.lang.String nullToEmpty​(java.lang.String input)
        Makes sure a given string is not null. If it is not null, the provided string is immediately returned. If it IS null, then an empty string is returned.
        Parameters:
        input - String which should be returned if not null
        Returns:
        The original string if not null, otherwise an empty string
        Since:
        4.2.0
      • emptyToNull

        public static java.lang.String emptyToNull​(java.lang.String input)
        Converts an empty string into a null. If it is not empty or null, the provided string is immediately returned.
        Parameters:
        input - String which should be returned if not null or empty
        Returns:
        The original string if not empty, otherwise null
        Since:
        4.2.0
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.String input)
        Check if the provided string is either null or empty.
        Parameters:
        input - String to check against
        Returns:
        true if the provided string is null or has no content
        Since:
        4.2.0
      • nonEmptyOptional

        public static java.util.Optional<java.lang.String> nonEmptyOptional​(java.lang.String input)
        Simple utility function for returning an Optional that if contents present are guaranteed to not be empty. Basically taking the empty case into consideration in addition to Optional.ofNullable(Object)'s normal null check.
        Parameters:
        input - String to be contained in returned Optional if not null or empty
        Returns:
        Optional which if present contains a non-empty String
        Since:
        5.4
      • padStart

        public static java.lang.String padStart​(java.lang.String sourceStr,
                                                int minLength,
                                                char padChar)
        Pads the start of the provided string with the provided character until a minimum length is reached. If the provided string is greater than or equal to the minLength it will be returned without modification. If the provided string is null it will be returned as an empty string, padded to the minimum length.
        Parameters:
        sourceStr - String to start from, this will be the end of the returned result string
        minLength - Minimum number of characters the returned string should be
        padChar - Character to pad on to the start of to reach the minimum length
        Returns:
        A non-null string that is at least the length requested
        Since:
        4.2.0
      • padEnd

        public static java.lang.String padEnd​(java.lang.String sourceStr,
                                              int minLength,
                                              char padChar)
        Pads the end of the provided string with the provided character until a minimum length is reached. If the provided string is greater than or equal to the minLength it will be returned without modification. If the provided string is null it will be returned as an empty string, padded to the minimum length.
        Parameters:
        sourceStr - String to start from, this will be the start of the returned result string
        minLength - Minimum number of characters the returned string should be
        padChar - Character to pad on to the end of to reach the minimum length
        Returns:
        A non-null string that is at least the length requested
        Since:
        4.2.0
      • makeRandomString

        public static java.lang.String makeRandomString​(int length)
        Produces a random string of the provided length. This can be useful for unit testing, or any other time the string content is not important. The returned string will be comprised of only alphanumeric characters.
        Parameters:
        length - Number of characters the resulting string should be.
        Returns:
        A string comprised of random characters of the specified length