Class StringUtils


  • public final class StringUtils
    extends Object

    Different Util methods for String

    Since:
    2.0
    Author:
    Andreas Pointner
    • Field Detail

      • INDEX_NOT_FOUND

        public static final int INDEX_NOT_FOUND
        Constant to define when an index is not found
        See Also:
        Constant Field Values
    • Method Detail

      • format

        @SafeVarargs
        public static <T> String format​(String s,
                                        Function<T,​String> extractor,
                                        T... replacements)
        Replaces REPLACEMENT_PLACEHOLDER with replacements
        Type Parameters:
        T - the type of the replacements
        Parameters:
        s - the string where the REPLACEMENT_PLACEHOLDER should be replaced
        extractor - the extractor function, that converts the replacement object into a string
        replacements - the replacement elements
        Returns:
        the string with the replace elements
        Throws:
        IllegalArgumentException - if the number of placeholders does not match the number of replacements
      • countMatches

        public static int countMatches​(String str,
                                       String sub)
        Counts how often sub is included in str
        Parameters:
        str - the string to be check
        sub - the sub string to be search in str
        Returns:
        the number of times sub occurs in str
      • indicesOf

        public static List<Integer> indicesOf​(String string,
                                              String substring)
        Returns the indices of every not overlapping occurrence of the substring in the given string
        Parameters:
        string - which contains the substring
        substring - which is contained in the string
        Returns:
        a list of all indices of every substring occurrence. List is empty if substring is not contained in string
      • indicesOf

        public static List<Integer> indicesOf​(String string,
                                              String substring,
                                              boolean dontOverlap)
        Returns the indices of every occurrence of the substring in the given string
        Parameters:
        string - which contains the substring
        substring - which is contained in the string
        dontOverlap - flag which decides if the occurrence of a substring can be overlap with another occurrence (e.g. bb in bbb could result in the indices 0 and 1 or just in 0 if overlapping is not allowed)
        Returns:
        a list of all indices of every substring occurrence. List is empty if substring is not contained in string
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(String stringToCheck)
        Returns if true if the given stringToCheck is either null or empty, as in String.isEmpty(), otherwise false
        Parameters:
        stringToCheck - the string to check for null or emptiness
        Returns:
        true if the passed string is null or empty
      • isNullOrBlank

        public static boolean isNullOrBlank​(String stringToCheck)
        Returns if true if the given stringToCheck is either null or blank, as in String.isBlank(), otherwise false
        Parameters:
        stringToCheck - the string to check for null or blankness
        Returns:
        true if the passed string is null or blank, meaning it only contains whitespaces
      • removeAll

        public static String removeAll​(@NonNull
                                       @NonNull String stringToCheck,
                                       @NonNull
                                       @NonNull Iterable<String> charsToRemove)
        Removes all occurrences of a the given Strings in the stringToCheck. the charsToRemove are iterated over and each element is used as a parameter in the String::replaceAll method
        Parameters:
        stringToCheck - the string, which contains characters that should be removed. If null, null is returned
        charsToRemove - a collection of string containing strings that need to be replaced in the stringToCheck parameter
        Returns:
        stringToCheck, where all charsToRemove have been replaced with the empty String
      • replaceAll

        public static String replaceAll​(@NonNull
                                        @NonNull String stringToCheck,
                                        @NonNull
                                        @NonNull Map<String,​String> replacementMapper)
        Replaces all occurrences of a the given Strings in the stringToCheck. the charsToRemove are iterated over and each key of the map is used as a the search parameter and each value as the replacement in the String::replace method.
        Parameters:
        stringToCheck - the string, which contains characters that should be removed. If null, null is returned
        replacementMapper - key value pairs describing how one string should be replaced. Key is the search term, value is the replacement string
        Returns:
        stringToCheck, where all charsToRemove have been replaced with the empty String