Class Strings


  • public final class Strings
    extends Object

    Convenience methods for manipulating strings.

    Author:
    David B. Bracewell
    • Method Detail

      • abbreviate

        public static String abbreviate​(CharSequence input,
                                        int length)

        Abbreviates a string to a desired length and adds "..." at the end.

        Parameters:
        input - The input string
        length - The length of the abbreviation
        Returns:
        The abbreviated string
      • appendIfNotPresent

        public static String appendIfNotPresent​(@NonNull
                                                @NonNull String string,
                                                @NonNull
                                                @NonNull String suffix)
        Append if not present string.
        Parameters:
        string - the string
        suffix - the suffix
        Returns:
        the string
      • center

        public static String center​(String s,
                                    int length)
        Centers an input string inside a string of given length
        Parameters:
        s - the string to center
        length - the length of the new string
        Returns:
        the centered string
      • count

        public static int count​(String str,
                                String target)
        Counts the number target strings in the given string
        Parameters:
        str - the str to calculate the count on
        target - the target which we want to count
        Returns:
        the number of times target occurs in str
      • emptyToNull

        public static String emptyToNull​(String input)
        Converts empty/bank strings to null
        Parameters:
        input - the input string
        Returns:
        the output string (null if input null or blank, input otherwise)
      • escape

        public static String escape​(String input,
                                    char escapeCharacter,
                                    @NonNull
                                    @NonNull String reservedCharacters)
        Escapes a string by placing an escape character in front of reserved characters.
        Parameters:
        input - the input
        escapeCharacter - the escape character
        reservedCharacters - the characters needing to be escaped
        Returns:
        the string
      • escape

        public static String escape​(String input,
                                    @NonNull
                                    @NonNull String escapeMarker,
                                    @NonNull
                                    @NonNull IntPredicate escapeChecker,
                                    @NonNull
                                    @NonNull IntFunction<String> conversionFunction)
        Escapes and converts characters matched using the given escapeChecker predicate by prepending the transformed value, determines using the given conversionFunction, with the escapeMarker.

         
               escape("MY @ time is great.",
                      "\\x",
                      c -> !Character.isLetter(c) && !Character.isWhitespace(c),
                      c -> Integer::toHexString);
         
         
        Generates MY \x40 time is great\x2e

        Parameters:
        input - the input string to escape
        escapeMarker - the escape string to use
        escapeChecker - the predicate to check if a character needs to be escaped
        conversionFunction - the function to convert the character needing escaped to a string.
        Returns:
        the escaped string
      • escapeUnicode

        public static String escapeUnicode​(String string)
        Escapes the unicode in the given string using the Java specification
        Parameters:
        string - The string to escape
        Returns:
        The escaped string
      • expand

        public static Span expand​(String txt,
                                  int start,
                                  int end)
        Expand int pair.
        Parameters:
        txt - the txt
        start - the start
        end - the end
        Returns:
        the int pair
      • firstMatch

        public static String firstMatch​(@NonNull
                                        @NonNull Pattern pattern,
                                        @NonNull
                                        @NonNull String input,
                                        int group)
      • firstMatch

        public static String firstMatch​(@NonNull
                                        @NonNull Pattern pattern,
                                        @NonNull
                                        @NonNull String input,
                                        @NonNull
                                        @NonNull String group)
      • firstMatch

        public static String firstMatch​(@NonNull
                                        @NonNull Pattern pattern,
                                        @NonNull
                                        @NonNull String input)
      • hasDigit

        public static boolean hasDigit​(CharSequence string)
        Determines if a string has at least one digit
        Parameters:
        string - the string to check
        Returns:
        True if the string has at least one digit
      • hasLetter

        public static boolean hasLetter​(CharSequence string)
        Determines if a string has at least one letter
        Parameters:
        string - the string to check
        Returns:
        True if the string has at least one letter
      • hasPunctuation

        public static boolean hasPunctuation​(CharSequence string)
        Determines if a given string has one or more punctuation characters.
        Parameters:
        string - the string to check
        Returns:
        True if the string has one or more punctuation characters
      • isAlphaNumeric

        public static boolean isAlphaNumeric​(CharSequence string)
        Determines if a string is only made up of letters or digits
        Parameters:
        string - the string to check
        Returns:
        True if the string is only made up of letter or digits
      • isDigit

        public static boolean isDigit​(CharSequence string)
        Determines if a string is only made up of numbers.
        Parameters:
        string - the string to check
        Returns:
        True if the string is only made up of numbers.
      • isLetter

        public static boolean isLetter​(CharSequence string)
        Determines if a string is only made up of letters.
        Parameters:
        string - the string to check
        Returns:
        True if the string is only made up of letters.
      • isLowerCase

        public static boolean isLowerCase​(CharSequence input)
        Determines if an entire string is lower case or not
        Parameters:
        input - The input string
        Returns:
        True if the string is lower case, False if not
      • isNonAlphaNumeric

        public static boolean isNonAlphaNumeric​(CharSequence string)
        Determines if a string is only made up of non letters and digits
        Parameters:
        string - the string to check
        Returns:
        True if the string is only made up of non letters and digits
      • isNotNullOrBlank

        public static boolean isNotNullOrBlank​(CharSequence input)
        Determines if a string is not null or blank (trimmed string is empty).
        Parameters:
        input - The input string
        Returns:
        True when the input string is not null and the trimmed version of the string is not empty.
      • isNullOrBlank

        public static boolean isNullOrBlank​(CharSequence input)
        Determines if a string is null or blank (trimmed string is empty).
        Parameters:
        input - The input string
        Returns:
        True when the input string is null or the trimmed version of the string is empty.
      • isPunctuation

        public static boolean isPunctuation​(CharSequence string)
        Determines if a given string is only made up of punctuation characters.
        Parameters:
        string - the string to check
        Returns:
        True if the string is all punctuation
      • isTitleCase

        public static boolean isTitleCase​(@NonNull
                                          @NonNull CharSequence input)
        Determines if an entire string is title case or not
        Parameters:
        input - The input string
        Returns:
        True if the string is title case, False if not
      • isUpperCase

        public static boolean isUpperCase​(CharSequence input)
        Determines if an entire string is upper case or not
        Parameters:
        input - The input string
        Returns:
        True if the string is upper case, False if not
      • join

        public static String join​(@NonNull
                                  @NonNull Iterable<?> iterable,
                                  @NonNull
                                  @NonNull CharSequence delimiter,
                                  @NonNull
                                  @NonNull CharSequence prefix,
                                  @NonNull
                                  @NonNull CharSequence suffix)
        Joins the items in the given iterable into a string separated using the given delimiter, with the given prefix at the beginning, and the given suffix at the end of the string.
        Parameters:
        iterable - the iterable
        delimiter - the delimiter
        prefix - the prefix
        suffix - the suffix
        Returns:
        the string
      • join

        public static String join​(@NonNull
                                  @NonNull Iterable<?> iterable,
                                  @NonNull
                                  @NonNull CharSequence delimiter)
        Joins the items in the given iterable into a string separated using the given delimiter.
        Parameters:
        iterable - the iterable
        delimiter - the delimiter
        Returns:
        the string
      • join

        public static <T> String join​(@NonNull
                                      @NonNull T[] values,
                                      @NonNull
                                      @NonNull CharSequence delimiter)
        Joins the items in the given array into a string separated using the given delimiter
        Type Parameters:
        T - the type parameter
        Parameters:
        values - the values
        delimiter - the delimiter
        Returns:
        the string
      • join

        public static <T> String join​(@NonNull
                                      @NonNull T[] values,
                                      @NonNull
                                      @NonNull CharSequence delimiter,
                                      @NonNull
                                      @NonNull CharSequence prefix,
                                      @NonNull
                                      @NonNull CharSequence suffix)
        Joins the items in the given array into a string separated using the given delimiter, with the given prefix at the beginning, and the given suffix at the end of the string.
        Type Parameters:
        T - the type parameter
        Parameters:
        values - the values
        delimiter - the delimiter
        prefix - the prefix
        suffix - the suffix
        Returns:
        the string
      • main

        public static void main​(String[] args)
      • nullToEmpty

        public static String nullToEmpty​(String input)
        Converts null values into an empty string
        Parameters:
        input - the input string
        Returns:
        the output string (empty if input null, input otherwise)
      • padEnd

        public static String padEnd​(CharSequence sequence,
                                    int desiredLength,
                                    char paddingCharacter)
        Pads the end of the string to make the string into the desired length using the given padding character to make up the additional length.
        Parameters:
        sequence - the sequence
        desiredLength - the desired length
        paddingCharacter - the padding character
        Returns:
        the string
      • padStart

        public static String padStart​(CharSequence sequence,
                                      int desiredLength,
                                      char paddingCharacter)
        Pads the beginning of the string to make the string into the desired length using the given padding character to make up the additional length.
        Parameters:
        sequence - the sequence
        desiredLength - the desired length
        paddingCharacter - the padding character
        Returns:
        the string
      • prependIfNotPresent

        public static String prependIfNotPresent​(@NonNull
                                                 @NonNull String string,
                                                 @NonNull
                                                 @NonNull String prefix)
        Prepend if not present string.
        Parameters:
        string - the string
        prefix - the prefix
        Returns:
        the string
      • randomHexString

        public static String randomHexString​(int length)
        Generates a random string of given length made up of valid hexadecimal characters.
        Parameters:
        length - the length of the string
        Returns:
        the random string
      • randomString

        public static String randomString​(int length,
                                          int min,
                                          int max)
        Generates a random string of a given length
        Parameters:
        length - The length of the string
        min - The min character in the string
        max - The max character in the string
        Returns:
        A string of random characters
      • randomString

        public static String randomString​(int length,
                                          CharMatcher validChar)
        Generates a random string of a given length
        Parameters:
        length - The length of the string
        validChar - CharPredicate that must match for a character to be returned in the string
        Returns:
        A string of random characters
      • randomString

        public static String randomString​(int length,
                                          int min,
                                          int max,
                                          CharMatcher validChar)
        Generates a random string of a given length
        Parameters:
        length - The length of the string
        min - The min character in the string
        max - The max character in the string
        validChar - CharPredicate that must match for a character to be returned in the string
        Returns:
        A string of random characters
      • removeDiacritics

        public static String removeDiacritics​(CharSequence input)
        Normalizes a string by removing the diacritics.
        Parameters:
        input - the input string
        Returns:
        Resulting string without diacritic marks
      • removeRepeatedChars

        public static String removeRepeatedChars​(CharSequence sequence)

        Replaces repeated characters with a single instance. e.g. Gooooood would become God.

        Parameters:
        sequence - The character sequence
        Returns:
        The compacted string
        Throws:
        NullPointerException - when the sequence is null
      • repeat

        public static String repeat​(String s,
                                    int count)
        Repeats the given string count times
        Parameters:
        s - the string to repeat
        count - the count
        Returns:
        the string
      • repeat

        public static String repeat​(char c,
                                    int count)
        Repeats the given character count times
        Parameters:
        c - the character to repeat
        count - the count
        Returns:
        the string
      • safeEquals

        public static boolean safeEquals​(String s1,
                                         String s2,
                                         boolean caseSensitive)
        Safe equals of two strings taking null into consideration.
        Parameters:
        s1 - the first string
        s2 - the second string
        caseSensitive - True if equals is case sensitive, False if case insensitive.
        Returns:
        the boolean
      • split

        public static List<String> split​(CharSequence input,
                                         char separator)
        Properly splits a delimited separated string using CSV assuming default values for the CSV object except for the delimiter.
        Parameters:
        input - The input string
        separator - The separator
        Returns:
        A list of all the cells in the input
      • toCanonicalForm

        public static String toCanonicalForm​(CharSequence input)
        Normalize to canonical form.
        Parameters:
        input - the input string
        Returns:
        the normalized string
      • toTitleCase

        public static String toTitleCase​(CharSequence input)
        Converts an input string to title case
        Parameters:
        input - The input string
        Returns:
        The title cased version of the input
      • unescape

        public static String unescape​(String input,
                                      char escapeCharacter)
        Unescapes a string which is escaped with the given escaped character.
        Parameters:
        input - the input
        escapeCharacter - the escape character
        Returns:
        the string