Class StringTools


  • public final class StringTools
    extends Object
    This class provides some useful string-related tool methods. The focus is on the ability to write short and simple code rather than on efficiency. This should be considered when using the methods to process large amounts of strings.
    • Field Detail

      • NUMBER_SUFFIX

        protected static final Pattern NUMBER_SUFFIX
        The pattern for the unique name number suffix.
    • Method Detail

      • nullToEmpty

        public static String nullToEmpty​(String string)
        Returns the empty string instead of null; otherwise the given string is returned.
        Parameters:
        string - a string or null
        Returns:
        the empty string instead of null; otherwise the given string
      • emptyToNull

        public static String emptyToNull​(String string)
        Returns null instead of the empty string; otherwise the given string is returned.
        Parameters:
        string - a string or null
        Returns:
        null instead of the empty string; otherwise the given string
      • trimToNull

        public static String trimToNull​(String string)
        Trims the designated string and if it is empty afterwards, null will be returned. This will also apply if the designated string is null.
        Parameters:
        string - The string which to trim.
        Returns:
        The trimmed string or null if the string is null or empty after trimming.
      • equal

        public static boolean equal​(String str1,
                                    Object str2)
        Returns whether two strings are equal. This method can deal with null values which is it's real benefit.

        Analogous to String.equals(Object) the right-hand parameter may be any object

        Parameters:
        str1 - a string or null
        str2 - another string or null; analogous to String.equals(Object) the right-hand parameter may actually be any object
        Returns:
        whether both strings are equal or both are null
      • join

        public static String join​(String glue,
                                  Object... tokens)
        Does the opposite of String.split(String).
        Parameters:
        glue - the string to put in between each pair of joined tokens
        tokens - the strings to be joined
        Returns:
        the joined string
      • join

        public static String join​(String glue,
                                  String... tokens)
        Does the opposite of String.split(String).
        Parameters:
        glue - the string to put in between each pair of joined tokens
        tokens - the strings to be joined
        Returns:
        the joined string
      • join

        public static String join​(String glue,
                                  Collection<?> tokens)
        Does the opposite of String.split(String).
        Parameters:
        glue - the string to put in between each pair of joined tokens
        tokens - the strings to be joined
        Returns:
        the joined string
      • join

        public static String join​(String glue,
                                  Iterator<String> tokens)
        Does the opposite of String.split(String).
        Parameters:
        glue - the string to put in between each pair of joined tokens
        tokens - the strings to be joined
        Returns:
        the joined string
      • join

        public static String[] join​(String glue,
                                    String[]... tokens)
        This method is equal to join(String, String...) except that it works on arrays of strings and not individual strings. So the first string of tokens[0] is joined with the first strings of tokens[1], tokens[2], ...; the second string of tokens[0] is joined with the second strings of tokens[1], tokens[2], ...; etc.;
        Parameters:
        glue - the string to put in between each pair of joined tokens
        tokens - the arrays of tokens to be joined
        Returns:
        an array of the joined strings
      • prefix

        public static String[] prefix​(String prefix,
                                      String... strings)
        Prepends the given prefix to each string and returns them in a new array.
        Parameters:
        strings - the strings to be prefixed
        prefix - the prefix
        Returns:
        a new array containing the prefixed strings
      • prefix

        public static List<String> prefix​(String prefix,
                                          Collection<String> strings)
        Prepends the given prefix to each string and returns them in a new list.
        Parameters:
        strings - the strings to be prefixed
        prefix - the prefix
        Returns:
        a new list containing the prefixed strings
      • suffix

        public static String[] suffix​(String suffix,
                                      String... strings)
        Appends the given suffix to each string and returns them in a new array.
        Parameters:
        strings - the strings to be suffixed
        suffix - the suffix
        Returns:
        a new array containing the suffixed strings
      • suffix

        public static List<String> suffix​(String suffix,
                                          Collection<String> strings)
        Appends the given suffix to each string and returns them in a new list.
        Parameters:
        strings - the strings to be suffixed
        suffix - the suffix
        Returns:
        a new list containing the suffixed strings
      • surround

        public static String[] surround​(String prefix,
                                        String suffix,
                                        String... strings)
        Surrounds the given strings with the specified prefix and suffix and returns them in a new array.
        Parameters:
        strings - the strings to be suffixed
        prefix - the prefix
        suffix - the suffix
        Returns:
        a new array containing the surrounded strings
      • surround

        public static List<String> surround​(String prefix,
                                            String suffix,
                                            List<String> strings)
        Surrounds the given strings with the specified prefix and suffix and returns them in a new list.
        Parameters:
        strings - the strings to be suffixed
        prefix - the prefix
        suffix - the suffix
        Returns:
        a new list containing the surrounded strings
      • repeat

        public static String repeat​(String string,
                                    int times)
        Returns a string representing the specified string repeated the specified number of times.
        Parameters:
        string - The string to be repeated (must not be null).
        times - the times the string should be repeated
        Returns:
        a string representing the specified string repeated the specified number of times
      • repeat

        public static String repeat​(String string,
                                    String glue,
                                    int times)
        Returns a string representing the specified string repeated the specified number of times. Between each repetition the specified glue string is inserted.
        Parameters:
        string - The string to be repeated (must not be null).
        glue - the string to appear between each repetition
        times - the times the string should be repeated
        Returns:
        a string representing the specified string repeated the specified number of times
      • pad

        public static String pad​(String string,
                                 int length)
        Returns the given string padded with spaces equally on both sides up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.
        Parameters:
        string - The string to be padded (may be null).
        length - the desired length after padding
        Returns:
        The given string padded with spaces equally on both sides up the given length. This will be null if the designated string is null.
      • lpad

        public static String lpad​(String string,
                                  int length)
        Returns the given string padded with spaces on the left up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.
        Parameters:
        string - The string to be padded (may be null).
        length - the desired length after padding
        Returns:
        The given string padded with spaces on the left up the given length. This will be null if the designated string is null.
      • rpad

        public static String rpad​(String string,
                                  int length)
        Returns the given string padded with spaces on the right up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.
        Parameters:
        string - The string to be padded (may be null).
        length - the desired length after padding
        Returns:
        The given string padded with spaces on the right up the given length. This will be null if the designated string is null.
      • pad

        public static String pad​(String string,
                                 int length,
                                 String padString)
        Returns the given string padded with characters from the specified pad string equally on both sides up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.
        Parameters:
        string - The string to be padded (may be null).
        length - the desired length after padding
        padString - the string containing the characters used for padding
        Returns:
        The given string padded with characters from the specified pad string equally on both sides up the given length. This will be null if the designated string is null.
      • lpad

        public static String lpad​(String string,
                                  int length,
                                  String padString)
        Returns the given string padded with characters from the specified pad string on the left up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.
        Parameters:
        string - The string to be padded (may be null).
        length - the desired length after padding
        padString - the string containing the characters used for padding
        Returns:
        The given string padded with characters from the specified pad string on the left up the given length. This will be null if the designated string is null.
      • rpad

        public static String rpad​(String string,
                                  int length,
                                  String padString)
        Returns the given string padded with characters from the specified pad string on the right up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.
        Parameters:
        string - The string to be padded (may be null).
        length - the desired length after padding
        padString - the string containing the characters used for padding
        Returns:
        The given string padded with characters from the specified pad string on the right up the given length. This will be null if the designated string is null.
      • replaceTabsAndNewlines

        public static String replaceTabsAndNewlines​(String string,
                                                    String replacement)
        Replace all tab and new line characters (\t, \r, \n, \r\n) from the designated string with the given replacement.
        Parameters:
        string - The string to be changed (may be null).
        replacement - The replacement that should be used for any tab and newline character.
        Returns:
        The string with all tabs and newlines replaced by the replacement string. This will be null if the designated string is null.
      • trim

        public static String trim​(String string)
        Removes all whitespace (as defined in Character.isWhitespace(char)) from both sides of the given string.
        Parameters:
        string - The string to be trimmed (may be null).
        Returns:
        The trimmed string or null if the designated string is null.
      • ltrim

        public static String ltrim​(String string)
        Removes all whitespace (as defined in Character.isWhitespace(char)) from the left side of the given string.
        Parameters:
        string - The string to be trimmed (may be null).
        Returns:
        The trimmed string or null if the designated string is null.
      • rtrim

        public static String rtrim​(String string)
        Removes all whitespace (as defined in Character.isWhitespace(char)) from the right side of the given string.
        Parameters:
        string - The string to be trimmed (may be null).
        Returns:
        The trimmed string or null if the designated string is null.
      • trim

        public static String trim​(String string,
                                  String trimChars)
        Removes all characters defined in trimChars from both sides of the given string.
        Parameters:
        string - The string to be trimmed (may be null).
        trimChars - the chars to be trimmed away from both sides of the string
        Returns:
        The trimmed string or null if the designated string is null.
      • ltrim

        public static String ltrim​(String string,
                                   String trimChars)
        Removes all characters defined in trimChars from the left side of the given string.
        Parameters:
        string - The string to be trimmed (may be null)
        trimChars - the chars to be trimmed away from the left side of the string
        Returns:
        The trimmed string or null if the designated string is null.
      • rtrim

        public static String rtrim​(String string,
                                   String trimChars)
        Removes all characters defined in trimChars from the right side of the given string.
        Parameters:
        string - The string to be trimmed (may be null).
        trimChars - the chars to be trimmed away from the right side of the string
        Returns:
        The trimmed string or null if the designated string is null.
      • trunc

        public static String trunc​(String string,
                                   int length)
        Truncate the string on his right side to the given length if necessary. Otherwise the same string is returned.
        Parameters:
        string - The string to be truncated (may be null).
        length - the desired length of the string
        Returns:
        The truncated string or null if the designated string is null.
      • toLower

        public static String[] toLower​(String[] strings)
        Converts all strings in the given array to lower case and returns them in a new array.
        Parameters:
        strings -
        Returns:
        lower case versions of the given strings
        See Also:
        String.toLowerCase()
      • toUpper

        public static String[] toUpper​(String[] strings)
        Converts all strings in the given array to upper case and returns them in a new array.
        Parameters:
        strings -
        Returns:
        upper case versions of the given strings
        See Also:
        String.toUpperCase()
      • toByteArray

        @Deprecated(since="15.0.0",
                    forRemoval=true)
        public static byte[] toByteArray​(String string,
                                         String encoding)
                                  throws UnsupportedEncodingException
        Deprecated, for removal: This API element is subject to removal in a future version.
        Encodes the given string to a byte array using the specified encoding.
        Parameters:
        string - the string to be encoded
        encoding - the encoding to be used
        Returns:
        the string encoded in a byte array using the specified encoding
        Throws:
        UnsupportedEncodingException - if the specified encoding is not supported
      • fromByteArray

        @Deprecated(since="15.0.0",
                    forRemoval=true)
        public static String fromByteArray​(byte[] bytes,
                                           String encoding)
                                    throws UnsupportedEncodingException
        Deprecated, for removal: This API element is subject to removal in a future version.
        Decodes the given byte array to a string using the specified encoding.
        Parameters:
        bytes - the byte array to be decoded
        encoding - the encoding to be used
        Returns:
        the string decoded from the byte array using the specified encoding
        Throws:
        UnsupportedEncodingException - if the specified encoding is not supported
      • fromStream

        public static String fromStream​(InputStream is,
                                        Charset encoding)
                                 throws IOException
        Decodes the designated input stream to a string using the specified encoding.
        Parameters:
        is - The input stream to decode to a string. This method will close the stream after reading.
        encoding - The encoding to be used.
        Returns:
        The string decoded from the designated input stream using the specified encoding.
        Throws:
        IOException - If there are problems reading from the stream, an IOException will be thrown.
      • splitClasspath

        public static void splitClasspath​(List<String> classPath)
        Splits each element of the designated list at ";". This allows classpaths to be either separated by "," (the list delimiter for Apache Commons Configuration) or ";" (the normal classpath separator in AristaFlow.
        The order is respected, that an element of the list having sub-elements leads to several additional list elements right after the corresponding element (depth-first).
        Parameters:
        classPath - The classpath as list to separate not yet separated classpaths.
      • hasValue

        public static boolean hasValue​(String s)
        Returns true, if the given String s actually has a value set, ie. it is not null and not an empty String.
        Parameters:
        s - The string to be checked.
        Returns:
        True if != null and != "".
      • defaultIfEmpty

        public static String defaultIfEmpty​(String s,
                                            String defaultValue)
        Returns s if it has value (not null and not the empty string), else returns defaultValue.
        Parameters:
        s - The string to checked for value and to be used if it has a value.
        defaultValue - The string to be used, if s is null or empty.
        Returns:
        s if it has a value, else defaultValue
      • intern

        public static String intern​(String s)
        Interns the designated string if we it is not null.
        Parameters:
        s - The string to intern or null.
        Returns:
        The interned string if interning is appropriate. Apparently, null will not be interned but just returned.
      • replace

        public static StringBuilder replace​(String del,
                                            String repl,
                                            StringBuilder sb)
        Replaces the designated string del by the designated string repl in the designated StringBuilder.
        Parameters:
        del - The string which to replace with the other string.
        repl - The string with which to replace the other string.
        sb - The StringBuilder in which to replace the string.
        Returns:
        The designated StringBuilder.
      • sanitise

        public static String sanitise​(String evilString)
        Removes bad characters from the designated string. This includes "u0000" which a lot of artefacts treat as string termination (Java does not do this).
        Parameters:
        evilString - The string which may contain bad characters.
        Returns:
        The designated string with the bad characters replaced.
      • hash

        public static String hash​(String string)
        Hashes the designated string with SHA-256.
        Parameters:
        string - The string to be hashed.
        Returns:
        The hashed string or null if the designated string is null.
      • getUniqueName

        public static String getUniqueName​(String name,
                                           Collection<String> allNames)
        Gets the designated string as unique name with respect to the designated name collection. Uniqueness is established by appending "-1" (or another number) add the end. Understandably, the number is being incremented by one until the name is unique.
        Parameters:
        name - The name which should be unique.
        allNames - The names already known. If this is null, the name will be returned unchanged.
        Returns:
        The designated name adapted so that it is unique with respect to the designated name collection.