Class StringUtils


  • public final class StringUtils
    extends org.apache.commons.lang.StringUtils
    Contains utility methods related to strings.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  StringUtils.WhiteSpaceTrimmingInputStream
      Replaces any chunk of whitespace characters within a text stream, with a single space character.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char BOUNDARY_CHARACTER
      The boundary character ("unit separator", 0x1F ASCII) (PLAT-4368)
      static java.lang.String UTF8_ENCODING
      UTF-8.
      • Fields inherited from class org.apache.commons.lang.StringUtils

        EMPTY, INDEX_NOT_FOUND
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String compressWhitespace​(java.lang.CharSequence s)
      Compresses whitespace.
      static java.lang.String compressWhitespace​(java.lang.String s, boolean keepBoundaryChars)
      Compresses whitespace.
      static java.lang.String encode​(long l)
      Encodes the value
      static java.lang.String encode​(java.lang.String s)
      Encodes s as JSON, escaping Unicode
      static java.lang.String encode​(java.lang.String value, boolean escapeUnicode)
      Encodes a value as JSON, with an option to escape Unicode
      static void escape​(java.lang.StringBuilder buffer, java.lang.String value)
      Write value to buffer, escaping double quote and backslash with a backslash.
      static int findMatchingClose​(java.lang.String startElem, java.lang.String endElem, java.lang.String text, int startIdx)
      Finds the matching close (identified by endElem), handling embedded opens (identified by startElem) and closes.
      static java.lang.String format​(java.lang.String value, java.lang.Object... args)
      Format value using args.
      static java.util.List<java.lang.String> getDelimitedStringValueList​(java.lang.String str, java.lang.String delim, boolean trim, boolean skipEmptyValues)
      Parses out a string using a delimiter into tokens and returns a string list filled with tokens, if any.
      static java.lang.String getFirst​(java.lang.String... vals)
      Gets the first non-blank value from a list of values, according to org.apache.commons.lang.StringUtils.isBlank().
      static java.lang.String getStackTraceAsString​(java.lang.Throwable th)
      Gets the stack trace for a given throwable as a string
      static java.util.List<java.lang.String> getTrimmedDelimitedStringValueList​(java.lang.CharSequence str, java.lang.String delim)
      Parses out a string using a delimiter into tokens and returns a string list filled with whitespace-trimmed tokens, if any.
      static byte[] getUtf8Bytes​(java.lang.String s)
      Gets bytes of string in utf8.
      static java.lang.String getUtf8String​(byte[] bytes)
      Gets string from utf8 bytes.
      static int indexOfNextLine​(java.lang.String str, int startIndex)
      Starting from startIndex, finds the index of the first character of the next line.
      static boolean isAsciiChar​(char c)
      Tells whether a given character is ASCII
      static java.lang.String join​(int[] ints, char sep, int count)
      Joins an array of ints into a string with a given separator.
      static java.lang.String join​(int[] ints, char sep, int start, int count)
      Joins an array of ints into a string with a given separator.
      static java.lang.String join​(int[] ints, java.lang.String sep)
      Joins an array of ints into a string with a given separator.
      static java.lang.String join​(int[] ints, java.lang.String sep, int count)
      Joins an array of ints into a string with a given separator.
      static java.lang.String join​(int[] ints, java.lang.String sep, int start, int count)
      Joins an array of ints into a string with a given separator.
      static java.lang.String join​(long[] longs, java.lang.String sep)
      Joins an array of longs into a string with a given separator.
      static java.lang.String join​(java.lang.String[] array, char sep, int startIndex, int endIndex)
      Join slice of array identified by startIndex and endIndex on sep.
      static java.lang.String join​(java.lang.String a, java.lang.String b, char sep)
      Join 2 strings on a separator character.
      static java.lang.String join​(java.util.List<java.lang.String> strList, java.lang.String separator)
      Joins input string list with separator
      static int lastIndexOfBefore​(java.lang.String src, java.lang.String lastTarget, java.lang.String beforeStr)
      Finds the last occurrence of lastTarget that appears before an occurrence of beforeStr.
      static java.lang.String millisecondsToString​(long time)
      Converts time in milliseconds to a user-friendly String that lists hours, minutes, seconds, and milliseconds.
      static byte[] parseIpAddress​(java.lang.String address)
      Parse a string representation of an ipv4 ip address into a byte array.
      static java.util.Map<java.lang.String,​java.lang.String[]> parseQueryString​(java.lang.String qs, java.lang.String encoding)
      Parses a standard uri query string.
      static java.lang.String quote​(java.lang.String value)
      Wrap value in double quotes.
      static void quote​(java.lang.StringBuilder buffer, java.lang.String value)
      Write the quoted string form of value to buffer.
      static java.lang.StringBuilder repeat​(java.lang.StringBuilder buffer, java.lang.String value, int count)
      Append value to buffer count times.
      static int[] splitIntArray​(java.lang.String data, char sep, int count)
      Split a String containing count decimal integers separated by sep.
      static java.lang.String stripOffInvalidXmlCharacters​(java.lang.String in, java.lang.String replacement)
      Ensures that the output String has only valid XML Unicode characters as specified by the XML 1.0 standard.
      static java.lang.String stripOffNonAlphanums​(java.lang.String str)
      Removes "bad" (non-alpha-numeric) characters from a String.
      static java.lang.String stripOffNonAlphanums​(java.lang.String str, java.lang.String okNonAlphaNumChars, java.lang.String replacement)
      Removes "bad" (non-alpha-numeric) characters from a String.
      static java.lang.String stripOffNonAscii​(java.lang.String in)
      Strips off any non-ASCII characters from a string
      static java.lang.String toTitleCase​(java.lang.String s)
      Converts the passed in string to the title case, i.e.
      static java.lang.String urlDecode​(java.lang.String s)
      Decode URL in UTF-8.
      static java.lang.String urlEncode​(java.lang.String s)
      Encode URL in UTF-8.
      • Methods inherited from class org.apache.commons.lang.StringUtils

        abbreviate, abbreviate, capitalise, capitaliseAllWords, capitalize, center, center, center, chomp, chomp, chompLast, chompLast, chop, chopNewline, clean, concatenate, contains, contains, containsAny, containsAny, containsIgnoreCase, containsNone, containsNone, containsOnly, containsOnly, countMatches, defaultIfEmpty, defaultString, defaultString, deleteSpaces, deleteWhitespace, difference, endsWith, endsWithIgnoreCase, equals, equalsIgnoreCase, escape, getChomp, getCommonPrefix, getLevenshteinDistance, getNestedString, getNestedString, getPrechomp, indexOf, indexOf, indexOf, indexOf, indexOfAny, indexOfAny, indexOfAny, indexOfAnyBut, indexOfAnyBut, indexOfDifference, indexOfDifference, isAlpha, isAlphanumeric, isAlphanumericSpace, isAlphaSpace, isAsciiPrintable, isBlank, isEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, join, join, join, join, join, join, join, join, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOfAny, left, leftPad, leftPad, leftPad, length, lowerCase, mid, ordinalIndexOf, overlay, overlayString, prechomp, remove, remove, removeEnd, removeEndIgnoreCase, removeStart, removeStartIgnoreCase, repeat, replace, replace, replaceChars, replaceChars, replaceEach, replaceEachRepeatedly, replaceOnce, reverse, reverseDelimited, reverseDelimitedString, right, rightPad, rightPad, rightPad, split, split, split, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithIgnoreCase, strip, strip, stripAll, stripAll, stripEnd, stripStart, stripToEmpty, stripToNull, substring, substring, substringAfter, substringAfterLast, substringBefore, substringBeforeLast, substringBetween, substringBetween, substringsBetween, swapCase, trim, trimToEmpty, trimToNull, uncapitalise, uncapitalize, upperCase
      • Methods inherited from class java.lang.Object

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

      • UTF8_ENCODING

        public static final java.lang.String UTF8_ENCODING
        UTF-8.
        See Also:
        Constant Field Values
      • BOUNDARY_CHARACTER

        public static final char BOUNDARY_CHARACTER
        The boundary character ("unit separator", 0x1F ASCII) (PLAT-4368)
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • format

        public static java.lang.String format​(java.lang.String value,
                                              java.lang.Object... args)
        Format value using args.

        Returns null if value is null.

      • stripOffInvalidXmlCharacters

        public static java.lang.String stripOffInvalidXmlCharacters​(java.lang.String in,
                                                                    java.lang.String replacement)
        Ensures that the output String has only valid XML Unicode characters as specified by the XML 1.0 standard. Replaces any invalid characters with the input replacement string; if the replacement string is null, the invalid characters are simply removed.
        Parameters:
        in - the String whose non-valid characters we want to remove.
        Returns:
        the input String, stripped of invalid characters, or an empty string if the input is null or empty
      • stripOffNonAlphanums

        public static java.lang.String stripOffNonAlphanums​(java.lang.String str)
        Removes "bad" (non-alpha-numeric) characters from a String. All UTF-16 alpha-numeric characters are retained. For example: stripOffNonAlphanums("ab$*^ab#%##ab", "%.,") yields "ababab".

        For historical reasons, the underscore character _ is currently not stripped. This will likely change.

        Parameters:
        str - the String to process
        Returns:
        the resulting String
        See Also:
        stripOffNonAlphanums(String,String,String)
      • stripOffNonAlphanums

        public static java.lang.String stripOffNonAlphanums​(java.lang.String str,
                                                            java.lang.String okNonAlphaNumChars,
                                                            java.lang.String replacement)
        Removes "bad" (non-alpha-numeric) characters from a String. All UTF-16 alpha-numeric characters are retained. Replaces non-alphanumeric characters with the passed in replacement string. If the replacement string is null or empty, the bad characters are removed without replacement. For example: stripOffNonAlphanums("ab$*^ab#%##ab", "%.,", "_") yields "ab_ab_%_ab". This version of the method takes an okNonAlphaNumChars argument which allows the caller to keep some non-alphanumerics in the resulting string. A series of non-ok characters will be replaced by a single replacement character.

        For historical reasons, the underscore character _ is currently not stripped. This will likely change, so we recommend you include underscore in okNonAlphaNumChars if that's the behavior you want.

        Parameters:
        str - the String to process
        okNonAlphaNumChars - the "OK" characters
        replacement - the replacement
        Returns:
        the resulting String
      • getTrimmedDelimitedStringValueList

        public static java.util.List<java.lang.String> getTrimmedDelimitedStringValueList​(java.lang.CharSequence str,
                                                                                          java.lang.String delim)
        Parses out a string using a delimiter into tokens and returns a string list filled with whitespace-trimmed tokens, if any. Null values are not returned (that is, if two delimiters are separated by only whitespace, the null result is skipped).
        Parameters:
        str - the string to parse
        delim - the delimiter
        Returns:
        string list filled with trimmed tokens or an empty list if none found
      • getDelimitedStringValueList

        public static java.util.List<java.lang.String> getDelimitedStringValueList​(java.lang.String str,
                                                                                   java.lang.String delim,
                                                                                   boolean trim,
                                                                                   boolean skipEmptyValues)
        Parses out a string using a delimiter into tokens and returns a string list filled with tokens, if any. Completely null values (i.e., no characters between delimiters) are always skipped.
        Parameters:
        str - the string to parse
        delim - the delimiter
        trim - whether to trim the leading and trailing whitespace off each token
        skipEmptyValues - whether to exclude values that are only whitespace. Remember that completely null values are always skipped regardless of the value of this parameter.
        Returns:
        string list filled with tokens or an empty list if none found
      • indexOfNextLine

        public static int indexOfNextLine​(java.lang.String str,
                                          int startIndex)
        Starting from startIndex, finds the index of the first character of the next line. Blank lines are skipped. The characters \r and \n define new lines. A line separator is considered to belong to the string after it, so indexOfNextLine("one\rtwo\nthree",3) returns 8 (the beginning of 'three'), not 4 (the beginning of 'two').
        Parameters:
        str - the string
        startIndex - the start index
        Returns:
        the index of the next line or -1 if no more lines
      • getStackTraceAsString

        public static java.lang.String getStackTraceAsString​(java.lang.Throwable th)
        Gets the stack trace for a given throwable as a string
        Parameters:
        th - the throwable
        Returns:
        the stack trace as a string
      • compressWhitespace

        public static java.lang.String compressWhitespace​(java.lang.CharSequence s)
        Compresses whitespace. This uses Java's Character.isWhitespace method so it specifically does not include non-breaking spaces.
        Parameters:
        s - String to compress
        Returns:
        string with leading and trailing whitespace removed, and internal runs of whitespace replaced by a single space character
      • compressWhitespace

        public static java.lang.String compressWhitespace​(java.lang.String s,
                                                          boolean keepBoundaryChars)
                                                   throws java.io.IOException
        Compresses whitespace.
        Parameters:
        s - String to compress
        keepBoundaryChars - if true, any boundary characters are retained, otherwise treated as whitespace and compressed
        Returns:
        string with leading and trailing whitespace removed, and internal runs of whitespace replaced by a single space character
        Throws:
        java.io.IOException
      • toTitleCase

        public static java.lang.String toTitleCase​(java.lang.String s)
        Converts the passed in string to the title case, i.e. capitalizes all the whitespace separated words in the string.
        Parameters:
        s - the string to convert
        Returns:
        the capitalized string
      • stripOffNonAscii

        public static java.lang.String stripOffNonAscii​(java.lang.String in)
        Strips off any non-ASCII characters from a string
        Parameters:
        in - the string
        Returns:
        the resulting string
      • isAsciiChar

        public static boolean isAsciiChar​(char c)
        Tells whether a given character is ASCII
        Parameters:
        c - the character to test
        Returns:
        true if the character is ASCII
      • findMatchingClose

        public static int findMatchingClose​(java.lang.String startElem,
                                            java.lang.String endElem,
                                            java.lang.String text,
                                            int startIdx)
        Finds the matching close (identified by endElem), handling embedded opens (identified by startElem) and closes. startIdx is where the starting elem is located. If 0, it will be searched for in text.
        Parameters:
        startElem -
        endElem -
        text -
        startIdx -
      • lastIndexOfBefore

        public static int lastIndexOfBefore​(java.lang.String src,
                                            java.lang.String lastTarget,
                                            java.lang.String beforeStr)
        Finds the last occurrence of lastTarget that appears before an occurrence of beforeStr.
        Parameters:
        src -
        lastTarget -
        beforeStr -
      • join

        public static java.lang.String join​(java.lang.String a,
                                            java.lang.String b,
                                            char sep)
        Join 2 strings on a separator character.
      • join

        public static java.lang.String join​(java.lang.String[] array,
                                            char sep,
                                            int startIndex,
                                            int endIndex)
        Join slice of array identified by startIndex and endIndex on sep. Throws NegativeArraySizeException if startIndex is less than or equal to endIndex.
      • join

        public static java.lang.String join​(int[] ints,
                                            java.lang.String sep)
        Joins an array of ints into a string with a given separator.
        Parameters:
        ints - the integers
        sep - the separator
        Returns:
        the joined string
      • join

        public static java.lang.String join​(long[] longs,
                                            java.lang.String sep)
        Joins an array of longs into a string with a given separator.
        Parameters:
        longs - the longs
        sep - the separator
        Returns:
        the joined string
      • join

        public static java.lang.String join​(int[] ints,
                                            java.lang.String sep,
                                            int count)
        Joins an array of ints into a string with a given separator.
        Parameters:
        ints - the integers
        sep - the separator
        Returns:
        the joined string
      • join

        public static java.lang.String join​(int[] ints,
                                            java.lang.String sep,
                                            int start,
                                            int count)
        Joins an array of ints into a string with a given separator.
        Parameters:
        ints - the integers
        sep - the separator
        count - only join this many ints
        Returns:
        the joined string
      • join

        public static java.lang.String join​(int[] ints,
                                            char sep,
                                            int count)
        Joins an array of ints into a string with a given separator.
        Parameters:
        ints - the integers
        sep - the separator
        count - only join this many ints
        Returns:
        the joined string
      • join

        public static java.lang.String join​(int[] ints,
                                            char sep,
                                            int start,
                                            int count)
        Joins an array of ints into a string with a given separator.
        Parameters:
        ints - the integers
        sep - the separator
        count - only join this many ints
        Returns:
        the joined string
      • join

        public static java.lang.String join​(java.util.List<java.lang.String> strList,
                                            java.lang.String separator)
        Joins input string list with separator
        Parameters:
        strList - string list to join
        separator - to use in between values
        Returns:
        strings joined by separator, or a zero length string if strList is null or empty
      • splitIntArray

        public static int[] splitIntArray​(java.lang.String data,
                                          char sep,
                                          int count)
        Split a String containing count decimal integers separated by sep.

        CAUTION: data is expected to be properly formatted and only contain characters '0' - '9', ',' and '-'.

      • getUtf8Bytes

        public static byte[] getUtf8Bytes​(java.lang.String s)
        Gets bytes of string in utf8.
      • getUtf8String

        public static java.lang.String getUtf8String​(byte[] bytes)
        Gets string from utf8 bytes.
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String s)
        Decode URL in UTF-8.
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String s)
        Encode URL in UTF-8.
      • parseIpAddress

        public static byte[] parseIpAddress​(java.lang.String address)
        Parse a string representation of an ipv4 ip address into a byte array.
      • parseQueryString

        public static java.util.Map<java.lang.String,​java.lang.String[]> parseQueryString​(java.lang.String qs,
                                                                                                java.lang.String encoding)
                                                                                         throws java.io.UnsupportedEncodingException
        Parses a standard uri query string. Handles null query strings.
        Parameters:
        qs -
        encoding -
        Returns:
        a map of query parameter to values. guaranteed to return non-null.
        Throws:
        java.io.UnsupportedEncodingException
      • encode

        public static java.lang.String encode​(long l)
        Encodes the value
        Parameters:
        l -
        Returns:
        the encoded value
      • encode

        public static java.lang.String encode​(java.lang.String s)
        Encodes s as JSON, escaping Unicode
        Parameters:
        s -
        Returns:
        the encoded value
      • encode

        public static java.lang.String encode​(java.lang.String value,
                                              boolean escapeUnicode)
        Encodes a value as JSON, with an option to escape Unicode
        Parameters:
        value - the value to encode
        escapeUnicode - the escape Unicode option
        Returns:
        the encoded string
      • quote

        public static java.lang.String quote​(java.lang.String value)
        Wrap value in double quotes.

        Any double quotes in value will be escaped by a backslash. Any backslashes in value will be escaped as double backslashes. No other escaping is done; for example a tab character is returned as simply a tab character, not backslash-t.

      • quote

        public static void quote​(java.lang.StringBuilder buffer,
                                 java.lang.String value)
        Write the quoted string form of value to buffer.

        Any double quotes and backslashes in value will be escaped by a backslash. No other escaping is done; for example a tab character is returned as simply a tab character, not backslash-t.

      • escape

        public static void escape​(java.lang.StringBuilder buffer,
                                  java.lang.String value)
        Write value to buffer, escaping double quote and backslash with a backslash.
      • repeat

        public static java.lang.StringBuilder repeat​(java.lang.StringBuilder buffer,
                                                     java.lang.String value,
                                                     int count)
        Append value to buffer count times. A negative value of count is silently treated as zero.
      • millisecondsToString

        public static java.lang.String millisecondsToString​(long time)
        Converts time in milliseconds to a user-friendly String that lists hours, minutes, seconds, and milliseconds.
        Parameters:
        time - the time in milliseconds.
        Returns:
        the user-friendly string
      • getFirst

        public static java.lang.String getFirst​(java.lang.String... vals)
        Gets the first non-blank value from a list of values, according to org.apache.commons.lang.StringUtils.isBlank(). Returns null if no non-blank values are found.