Class StringUtil


  • public final class StringUtil
    extends Object
    String-related utility functions. See also StringUtils.
    Author:
    BrianRemedios, Clément Fournier
    • Method Detail

      • inSingleQuotes

        public static String inSingleQuotes​(String s)
      • inDoubleQuotes

        public static @NonNull String inDoubleQuotes​(String expected)
      • lineNumberAt

        public static int lineNumberAt​(CharSequence charSeq,
                                       int offsetInclusive)
        Returns the (1-based) line number of the character at the given index. Line terminators (\r, \n) are assumed to be on the line they *end* and not on the following line. The method also accepts that the given offset be the length of the string (in which case there's no targeted character), to get the line number of a character that would be inserted at the end of the string.
        
             lineNumberAt("a\nb", 0)  = 1
             lineNumberAt("a\nb", 1)  = 1
             lineNumberAt("a\nb", 2)  = 2
             lineNumberAt("a\nb", 3)  = 2  // charAt(3) doesn't exist though
             lineNumberAt("a\nb", 4)  = -1
        
             lineNumberAt("", 0) = 1
             lineNumberAt("", _) = -1
        
         
        Parameters:
        charSeq - Char sequence
        offsetInclusive - Offset in the sequence of the targeted character. May be the length of the sequence.
        Returns:
        -1 if the offset is not in [0, length], otherwise the line number
      • columnNumberAt

        public static int columnNumberAt​(CharSequence charSeq,
                                         int offsetInclusive)
        Returns the (1-based) column number of the character at the given index. Line terminators are by convention taken to be part of the line they end, and not the new line they start. Each character has width 1 (including \t). The method also accepts that the given offset be the length of the string (in which case there's no targeted character), to get the column number of a character that would be inserted at the end of the string.
        
             columnNumberAt("a\nb", 0)  = 1
             columnNumberAt("a\nb", 1)  = 2
             columnNumberAt("a\nb", 2)  = 1
             columnNumberAt("a\nb", 3)  = 2   // charAt(3) doesn't exist though
             columnNumberAt("a\nb", 4)  = -1
        
             columnNumberAt("a\r\n", 2)  = 3
        
         
        Parameters:
        charSeq - Char sequence
        offsetInclusive - Offset in the sequence
        Returns:
        -1 if the offset is not in [0, length], otherwise the column number
      • substringAfterLast

        public static String substringAfterLast​(String str,
                                                int c)
        Returns the substring following the last occurrence of the given character. If the character doesn't occur, returns the whole string. This contrasts with StringUtils.substringAfterLast(String, String), which returns the empty string in that case.
        Parameters:
        str - String to cut
        c - Delimiter
      • percentageString

        public static String percentageString​(double val,
                                              int numDecimals)
        Formats a double to a percentage, keeping numDecimal decimal places.
        Parameters:
        val - a double value between 0 and 1
        numDecimals - The number of decimal places to keep
        Returns:
        A formatted string
        Throws:
        IllegalArgumentException - if the double to format is not between 0 and 1
      • withoutPrefixes

        public static String withoutPrefixes​(String text,
                                             String... prefixes)
        Checks for the existence of any of the listed prefixes on the non-null text and removes them.
        Returns:
        String
      • removedInvalidXml10Characters

        public static String removedInvalidXml10Characters​(String text)
        Remove characters, that are not allowed in XML 1.0 documents.

        Allowed characters are:

        Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
        (see Extensible Markup Language (XML) 1.0 (Fifth Edition)).
      • escapeWhitespace

        public static String escapeWhitespace​(Object o)
        Replace some whitespace characters so they are visually apparent.
        Returns:
        String
      • linesWithTrimIndent

        public static List<Chars> linesWithTrimIndent​(String source)
        Returns a list of
      • trimIndentInPlace

        public static void trimIndentInPlace​(List<Chars> lines)
        Trim the common indentation of each line in place in the input list. Trailing whitespace is removed on each line. Note that blank lines do not count towards computing the max common indentation, except the last one.
        Parameters:
        lines - mutable list
      • isSame

        public static boolean isSame​(String s1,
                                     String s2,
                                     boolean trim,
                                     boolean ignoreCase,
                                     boolean standardizeWhitespace)
        Are the two String values the same. The Strings can be optionally trimmed before checking. The Strings can be optionally compared ignoring case. The Strings can be have embedded whitespace standardized before comparing. Two null values are treated as equal.
        Parameters:
        s1 - The first String.
        s2 - The second String.
        trim - Indicates if the Strings should be trimmed before comparison.
        ignoreCase - Indicates if the case of the Strings should ignored during comparison.
        standardizeWhitespace - Indicates if the embedded whitespace should be standardized before comparison.
        Returns:
        true if the Strings are the same, false otherwise.
      • asString

        public static String asString​(Object[] items,
                                      String separator)
        Formats all items onto a string with separators if more than one exists, return an empty string if the items are null or empty.
        Parameters:
        items - Object[]
        separator - String
        Returns:
        String
      • removeSurrounding

        public static String removeSurrounding​(String string,
                                               char delimiter)
        If the string starts and ends with the delimiter, returns the substring within the delimiters. Otherwise returns the original string. The start and end delimiter must be 2 separate instances.
        
         removeSurrounding("",     _ )  = ""
         removeSurrounding("q",   'q')  = "q"
         removeSurrounding("qq",  'q')  = ""
         removeSurrounding("q_q", 'q')  = "_"
         
      • elide

        public static String elide​(String string,
                                   int maxOutputLength,
                                   String ellipsis)
        Truncate the given string to some maximum length. If it needs truncation, the ellipsis string is appended. The length of the returned string is always lower-or-equal to the maxOutputLength, even when truncation occurs.
      • escapeJava

        public static String escapeJava​(String str)
        Replaces unprintable characters by their escaped (or unicode escaped) equivalents in the given string
      • quoteMessageFormat

        public static String quoteMessageFormat​(String str)
        Escape the string so that it appears literally when interpreted by a MessageFormat.
      • nullToEmpty

        public static String nullToEmpty​(String value)
        Return the empty string if the parameter is null.