Class StringUtils


  • @PublicEvolving
    public final class StringUtils
    extends Object
    Utility class to convert objects into strings in vice-versa.
    • Field Detail

      • EMPTY_STRING_ARRAY

        public static final String[] EMPTY_STRING_ARRAY
        An empty string array. There are just too many places where one needs an empty string array and wants to save some object allocation.
    • Method Detail

      • byteToHexString

        public static String byteToHexString​(byte[] bytes,
                                             int start,
                                             int end)
        Given an array of bytes it will convert the bytes to a hex string representation of the bytes.
        Parameters:
        bytes - the bytes to convert in a hex string
        start - start index, inclusively
        end - end index, exclusively
        Returns:
        hex string representation of the byte array
      • byteToHexString

        public static String byteToHexString​(byte[] bytes)
        Given an array of bytes it will convert the bytes to a hex string representation of the bytes.
        Parameters:
        bytes - the bytes to convert in a hex string
        Returns:
        hex string representation of the byte array
      • hexStringToByte

        public static byte[] hexStringToByte​(String hex)
        Given a hex string this will return the byte array corresponding to the string .
        Parameters:
        hex - the hex String array
        Returns:
        a byte array that is a hex string representation of the given string. The size of the byte array is therefore hex.length/2
      • showControlCharacters

        public static String showControlCharacters​(String str)
        Replaces control characters by their escape-coded version. For example, if the string contains a line break character ('\n'), this character will be replaced by the two characters backslash '\' and 'n'. As a consequence, the resulting string will not contain any more control characters.
        Parameters:
        str - The string in which to replace the control characters.
        Returns:
        The string with the replaced characters.
      • getRandomString

        public static String getRandomString​(Random rnd,
                                             int minLength,
                                             int maxLength)
        Creates a random string with a length within the given interval. The string contains only characters that can be represented as a single code point.
        Parameters:
        rnd - The random used to create the strings.
        minLength - The minimum string length.
        maxLength - The maximum string length (inclusive).
        Returns:
        A random String.
      • getRandomString

        public static String getRandomString​(Random rnd,
                                             int minLength,
                                             int maxLength,
                                             char minValue,
                                             char maxValue)
        Creates a random string with a length within the given interval. The string contains only characters that can be represented as a single code point.
        Parameters:
        rnd - The random used to create the strings.
        minLength - The minimum string length.
        maxLength - The maximum string length (inclusive).
        minValue - The minimum character value to occur.
        maxValue - The maximum character value to occur.
        Returns:
        A random String.
      • generateRandomAlphanumericString

        public static String generateRandomAlphanumericString​(Random rnd,
                                                              int length)
        Creates a random alphanumeric string of given length.
        Parameters:
        rnd - The random number generator to use.
        length - The number of alphanumeric characters to append.
      • writeString

        public static void writeString​(@Nonnull
                                       String str,
                                       DataOutputView out)
                                throws IOException
        Writes a String to the given output. The written string can be read with readString(DataInputView).
        Parameters:
        str - The string to write
        out - The output to write to
        Throws:
        IOException - Thrown, if the writing or the serialization fails.
      • readString

        public static String readString​(DataInputView in)
                                 throws IOException
        Reads a non-null String from the given input.
        Parameters:
        in - The input to read from
        Returns:
        The deserialized String
        Throws:
        IOException - Thrown, if the reading or the deserialization fails.
      • writeNullableString

        public static void writeNullableString​(@Nullable
                                               String str,
                                               DataOutputView out)
                                        throws IOException
        Writes a String to the given output. The string may be null. The written string can be read with readNullableString(DataInputView)-
        Parameters:
        str - The string to write, or null.
        out - The output to write to.
        Throws:
        IOException - Thrown, if the writing or the serialization fails.
      • isNullOrWhitespaceOnly

        public static boolean isNullOrWhitespaceOnly​(String str)
        Checks if the string is null, empty, or contains only whitespace characters. A whitespace character is defined via Character.isWhitespace(char).
        Parameters:
        str - The string to check
        Returns:
        True, if the string is null or blank, false otherwise.
      • concatenateWithAnd

        @Nullable
        public static String concatenateWithAnd​(@Nullable
                                                String s1,
                                                @Nullable
                                                String s2)
        If both string arguments are non-null, this method concatenates them with ' and '. If only one of the arguments is non-null, this method returns the non-null argument. If both arguments are null, this method returns null.
        Parameters:
        s1 - The first string argument
        s2 - The second string argument
        Returns:
        The concatenated string, or non-null argument, or null
      • toQuotedListString

        public static String toQuotedListString​(Object[] values)
        Generates a string containing a comma-separated list of values in double-quotes. Uses lower-cased values returned from Object.toString() method for each element in the given array. Null values are skipped.
        Parameters:
        values - array of elements for the list
        Returns:
        The string with quoted list of elements