Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String capitalize​(java.lang.String value)  
      static java.lang.String commonMargin​(@Nullable java.lang.CharSequence s1, java.lang.CharSequence s2)  
      static int countOccurrences​(@NonNull java.lang.String text, @NonNull java.lang.String substring)
      Return the number of times a substring occurs within a target string.
      static java.lang.String ensureNewlineCountBeforeComment​(java.lang.String text, int desiredNewlineCount)
      Return a copy of the supplied text that contains exactly the desired number of newlines appear before characters that indicate the beginning of a C-style comment, "//" and "/*".
      static java.lang.String greatestCommonMargin​(java.lang.String multiline)
      Locate the greatest common margin of a multi-line string
      static java.lang.String indent​(java.lang.String text)  
      static int indexOf​(java.lang.String text, java.util.function.Predicate<java.lang.Character> test)  
      static int indexOfNonWhitespace​(java.lang.String text)  
      static boolean isBlank​(@Nullable java.lang.String string)
      Check if the String is null or has only whitespaces.
      static boolean isNullOrEmpty​(@Nullable java.lang.String string)
      Check if the String is empty string or null.
      static boolean isNumeric​(java.lang.String str)  
      static boolean matchesGlob​(@Nullable java.lang.String value, @Nullable java.lang.String globPattern)  
      static int mostCommonIndent​(java.util.SortedMap<java.lang.Integer,​java.lang.Long> indentFrequencies)  
      static java.lang.String readFully​(java.io.InputStream inputStream)  
      static java.lang.String repeat​(java.lang.String s, int count)  
      static java.lang.String replaceFirst​(@NonNull java.lang.String text, @NonNull java.lang.String match, @NonNull java.lang.String replacement)
      This method will search and replace the first occurrence of a matching substring.
      static java.util.List<java.lang.String> splitCStyleComments​(java.lang.String text)
      Given a prefix comprised of segments that are exclusively whitespace, single line comments, or multi-line comments, return a list of each segment.
      static java.lang.String trimIndent​(java.lang.String text)  
      static java.lang.String uncapitalize​(java.lang.String value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • trimIndent

        public static java.lang.String trimIndent​(java.lang.String text)
      • mostCommonIndent

        public static int mostCommonIndent​(java.util.SortedMap<java.lang.Integer,​java.lang.Long> indentFrequencies)
      • isBlank

        public static boolean isBlank​(@Nullable
                                      @Nullable java.lang.String string)
        Check if the String is null or has only whitespaces.

        Modified from apache commons lang StringUtils.

        Parameters:
        string - String to check
        Returns:
        true if the String is null or has only whitespaces
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(@Nullable
                                            @Nullable java.lang.String string)
        Check if the String is empty string or null.
        Parameters:
        string - String to check
        Returns:
        true if the String is null or empty string
      • readFully

        public static java.lang.String readFully​(java.io.InputStream inputStream)
      • capitalize

        public static java.lang.String capitalize​(java.lang.String value)
      • uncapitalize

        public static java.lang.String uncapitalize​(java.lang.String value)
      • splitCStyleComments

        public static java.util.List<java.lang.String> splitCStyleComments​(java.lang.String text)
        Given a prefix comprised of segments that are exclusively whitespace, single line comments, or multi-line comments, return a list of each segment.

        Operates on C-style comments: // single line /* multi-line

        If the provided input contains anything except whitespace and c-style comments this will probably explode

        Parameters:
        text - The space to split
        Returns:
        A list of raw whitespace, single line comments, and multi-line comments
      • ensureNewlineCountBeforeComment

        public static java.lang.String ensureNewlineCountBeforeComment​(java.lang.String text,
                                                                       int desiredNewlineCount)
        Return a copy of the supplied text that contains exactly the desired number of newlines appear before characters that indicate the beginning of a C-style comment, "//" and "/*".

        If the supplied text does not contain a comment indicator then this is equivalent to calling ensureNewlineCount()

        Parameters:
        text - Original text to add newlines to
        desiredNewlineCount - The number of newlines that should be before the text
        Returns:
        A copy of the supplied text with the desired number of newlines
      • indexOfNonWhitespace

        public static int indexOfNonWhitespace​(java.lang.String text)
      • indexOf

        public static int indexOf​(java.lang.String text,
                                  java.util.function.Predicate<java.lang.Character> test)
        Parameters:
        text - Text to scan
        test - The predicate to match
        Returns:
        The index of the first character for which the predicate returns true, or -1 if no character in the string matches the predicate.
      • countOccurrences

        public static int countOccurrences​(@NonNull
                                           @NonNull java.lang.String text,
                                           @NonNull
                                           @NonNull java.lang.String substring)
        Return the number of times a substring occurs within a target string.
        Parameters:
        text - A target string
        substring - The substring to search for
        Returns:
        the number of times the substring is found in the target. 0 if no occurances are found.
      • replaceFirst

        public static java.lang.String replaceFirst​(@NonNull
                                                    @NonNull java.lang.String text,
                                                    @NonNull
                                                    @NonNull java.lang.String match,
                                                    @NonNull
                                                    @NonNull java.lang.String replacement)
        This method will search and replace the first occurrence of a matching substring. There is a a replaceFirst method on the String class but that version leverages regular expressions and is a magnitude slower than this simple replacement.
        Parameters:
        text - The source string to search
        match - The substring that is being searched for
        replacement - The replacement.
        Returns:
        The original string with the first occurrence replaced or the original text if a match is not found.
      • repeat

        public static java.lang.String repeat​(java.lang.String s,
                                              int count)
      • indent

        public static java.lang.String indent​(java.lang.String text)
      • greatestCommonMargin

        public static java.lang.String greatestCommonMargin​(java.lang.String multiline)
        Locate the greatest common margin of a multi-line string
        Parameters:
        multiline - A string of one or more lines.
        Returns:
        The greatest common margin consisting only of whitespace characters.
      • commonMargin

        public static java.lang.String commonMargin​(@Nullable
                                                    @Nullable java.lang.CharSequence s1,
                                                    java.lang.CharSequence s2)
      • isNumeric

        public static boolean isNumeric​(java.lang.String str)