Class Strings


  • public final class Strings
    extends java.lang.Object
    A variety of static String utility methods.

    The escapeMarkup() and toMultilineMarkup() methods are useful for turning normal Java Strings into HTML strings.

    The lastPathComponent(), firstPathComponent(), afterFirstPathComponent() and beforeLastPathComponent() methods can chop up a String into path components using a separator character. If the separator cannot be found the original String is returned.

    Similarly, the beforeLast(), beforeFirst(), afterFirst() and afterLast() methods return sections before and after a separator character. But if the separator cannot be found, an empty string is returned.

    Some other miscellaneous methods will strip a given ending off a String if it can be found (stripEnding()), replace all occurrences of one String with another (replaceAll), do type conversions (toBoolean(), toChar(), toString()), check a String for emptiness (isEmpty()), convert a Throwable to a String (toString(Throwable)) or capitalize a String (capitalize()).

    Author:
    Jonathan Locke
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String LINE_SEPARATOR
      The line separator for the current platform.
      static java.lang.String SESSION_ID_PARAM_NAME
      The name of the parameter used to keep the session id.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String afterFirst​(java.lang.String s, char c)
      Returns everything after the first occurrence of the given character in s.
      static java.lang.String afterFirstPathComponent​(java.lang.String path, char separator)
      Gets everything after the first path component of a path using a given separator.
      static java.lang.String afterLast​(java.lang.String s, char c)
      Returns everything after the last occurrence of the given character in s.
      static java.lang.String beforeFirst​(java.lang.String s, char c)
      Returns everything before the first occurrence of the given character in s.
      static java.lang.String beforeLast​(java.lang.String s, char c)
      Returns everything before the last occurrence of the given character in s.
      static java.lang.String beforeLastPathComponent​(java.lang.String path, char separator)
      Gets everything before the last path component of a path using a given separator.
      static java.lang.String capitalize​(java.lang.String s)
      Capitalizes a string.
      static java.lang.String defaultIfEmpty​(java.lang.String originalString, java.lang.String defaultValue)
      Returns the original string if this one is not empty (i.e.
      static java.lang.CharSequence escapeMarkup​(java.lang.CharSequence s)
      Converts a Java String to an HTML markup string, but does not convert normal spaces to non-breaking space entities (<nbsp>).
      static java.lang.CharSequence escapeMarkup​(java.lang.CharSequence s, boolean escapeSpaces)
      Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate.
      static java.lang.CharSequence escapeMarkup​(java.lang.CharSequence s, boolean escapeSpaces, boolean convertToHtmlUnicodeEscapes)
      Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate.
      static java.lang.String firstPathComponent​(java.lang.String path, char separator)
      Gets the first path component of a path using a given separator.
      static java.lang.String fromEscapedUnicode​(java.lang.String escapedUnicodeString)
      Converts encoded \uxxxx to unicode chars and changes special saved chars to their original forms.
      static int getLevenshteinDistance​(java.lang.CharSequence s, java.lang.CharSequence t)
      Find the Levenshtein distance between two Strings.
      static int indexOf​(java.lang.CharSequence sequence, char ch)
      returns the zero-based index of a character within a char sequence.
      static boolean isEmpty​(java.lang.CharSequence string)
      Checks whether the string is considered empty.
      static boolean isEqual​(java.lang.String string1, java.lang.String string2)
      Checks whether two strings are equals taken care of 'null' values and treating 'null' same as trim(string).equals("")
      static boolean isTrue​(java.lang.String s)
      Converts the text in s to a corresponding boolean.
      static java.lang.String join​(java.lang.String separator, java.lang.String... fragments)
      Joins string fragments using the specified separator
      static java.lang.String join​(java.lang.String separator, java.util.List<java.lang.String> fragments)
      Joins string fragments using the specified separator
      static java.lang.String lastPathComponent​(java.lang.String path, char separator)
      Gets the last path component of a path using a given separator.
      static int lengthInBytes​(java.lang.String string, java.nio.charset.Charset charset)
      Calculates the length of string in bytes, uses specified charset if provided.
      static java.lang.CharSequence replaceAll​(java.lang.CharSequence s, java.lang.CharSequence searchFor, java.lang.CharSequence replaceWith)
      Replace all occurrences of one string replaceWith another string.
      static java.lang.String replaceHtmlEscapeNumber​(java.lang.String str)
      Replace HTML numbers like &#20540; by the appropriate character.
      static java.lang.String[] split​(java.lang.String s, char c)
      Simpler, faster version of String.split() for splitting on a simple character.
      static boolean startsWith​(java.lang.String str, java.lang.String prefix, boolean caseSensitive)
      Extended String.startsWith(String) with support for case sensitivity
      static java.lang.String stripEnding​(java.lang.String s, java.lang.String ending)
      Strips the ending from the string s.
      static java.lang.String stripJSessionId​(java.lang.String url)
      Strip any jsessionid and possibly other redundant info that might be in our way.
      static java.lang.Boolean toBoolean​(java.lang.String s)
      Converts the string s to a Boolean.
      static char toChar​(java.lang.String s)
      Converts the 1 character string s to a character.
      static <T extends java.lang.Enum<T>>
      T
      toEnum​(java.lang.CharSequence value, java.lang.Class<T> enumClass)
      Return this value as en enum value.
      static java.lang.String toEscapedUnicode​(java.lang.String unicodeString)
      Converts unicodes to encoded \uxxxx.
      static java.lang.String toHexString​(byte[] bytes)
      convert byte array to hex string
      static java.lang.CharSequence toMultilineMarkup​(java.lang.CharSequence s)
      Converts a String to multiline HTML markup by replacing newlines with line break entities (<br/>) and multiple occurrences of newline with paragraph break entities (<p>).
      static java.lang.String toString​(java.lang.Object object)
      Converts the given object to a string.
      static java.lang.String toString​(java.lang.Throwable throwable)
      Converts a Throwable to a string.
      static java.lang.CharSequence unescapeMarkup​(java.lang.String markup)
      Unescapes the escaped entities in the markup passed.
      • Methods inherited from class java.lang.Object

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

      • LINE_SEPARATOR

        public static final java.lang.String LINE_SEPARATOR
        The line separator for the current platform.
      • SESSION_ID_PARAM_NAME

        public static final java.lang.String SESSION_ID_PARAM_NAME
        The name of the parameter used to keep the session id. The Servlet specification mandates jsessionid but the web containers provide ways to set a custom one, e.g. sid. Since Wicket doesn't have access to the web container internals the name should be set explicitly.
    • Method Detail

      • afterFirst

        public static java.lang.String afterFirst​(java.lang.String s,
                                                  char c)
        Returns everything after the first occurrence of the given character in s.
        Parameters:
        s - The string
        c - The character
        Returns:
        Everything after the first occurrence of the given character in s. If the character cannot be found, an empty string is returned.
      • afterFirstPathComponent

        public static java.lang.String afterFirstPathComponent​(java.lang.String path,
                                                               char separator)
        Gets everything after the first path component of a path using a given separator. If the separator cannot be found, an empty String is returned.

        For example, afterFirstPathComponent("foo:bar:baz", ':') would return "bar:baz" and afterFirstPathComponent("foo", ':') would return "".

        Parameters:
        path - The path to parse
        separator - The path separator character
        Returns:
        Everything after the first component in the path
      • afterLast

        public static java.lang.String afterLast​(java.lang.String s,
                                                 char c)
        Returns everything after the last occurrence of the given character in s.
        Parameters:
        s - The string
        c - The character
        Returns:
        Everything after the last occurrence of the given character in s. If the character cannot be found, an empty string is returned.
      • beforeFirst

        public static java.lang.String beforeFirst​(java.lang.String s,
                                                   char c)
        Returns everything before the first occurrence of the given character in s.
        Parameters:
        s - The string
        c - The character
        Returns:
        Everything before the first occurrence of the given character in s. If the character cannot be found, an empty string is returned.
      • beforeLast

        public static java.lang.String beforeLast​(java.lang.String s,
                                                  char c)
        Returns everything before the last occurrence of the given character in s.
        Parameters:
        s - The string
        c - The character
        Returns:
        Everything before the last occurrence of the given character in s. If the character cannot be found, an empty string is returned.
      • beforeLastPathComponent

        public static java.lang.String beforeLastPathComponent​(java.lang.String path,
                                                               char separator)
        Gets everything before the last path component of a path using a given separator. If the separator cannot be found, the path itself is returned.

        For example, beforeLastPathComponent("foo.bar.baz", '.') would return "foo.bar" and beforeLastPathComponent("foo", '.') would return "".

        Parameters:
        path - The path to parse
        separator - The path separator character
        Returns:
        Everything before the last component in the path
      • capitalize

        public static java.lang.String capitalize​(java.lang.String s)
        Capitalizes a string.
        Parameters:
        s - The string
        Returns:
        The capitalized string
      • escapeMarkup

        public static java.lang.CharSequence escapeMarkup​(java.lang.CharSequence s)
        Converts a Java String to an HTML markup string, but does not convert normal spaces to non-breaking space entities (<nbsp>).
        Parameters:
        s - The characters to escape
        Returns:
        The escaped string
        See Also:
        escapeMarkup(CharSequence, boolean)
      • escapeMarkup

        public static java.lang.CharSequence escapeMarkup​(java.lang.CharSequence s,
                                                          boolean escapeSpaces)
        Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate. Spaces are converted to non-breaking spaces (<nbsp>) if escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are converted to &lt; entities and greater than signs to &gt; entities.
        Parameters:
        s - The characters to escape
        escapeSpaces - True to replace ' ' with nonbreaking space
        Returns:
        The escaped string
      • escapeMarkup

        public static java.lang.CharSequence escapeMarkup​(java.lang.CharSequence s,
                                                          boolean escapeSpaces,
                                                          boolean convertToHtmlUnicodeEscapes)
        Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate. Spaces are converted to non-breaking spaces (<nbsp>) if escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are converted to &lt; entities and greater than signs to &gt; entities.
        Parameters:
        s - The characters to escape
        escapeSpaces - True to replace ' ' with nonbreaking space
        convertToHtmlUnicodeEscapes - True to convert non-7 bit characters to unicode HTML (&#...)
        Returns:
        The escaped string
      • unescapeMarkup

        public static java.lang.CharSequence unescapeMarkup​(java.lang.String markup)
        Unescapes the escaped entities in the markup passed.
        Parameters:
        markup - The source String to unescape.
        Returns:
        the unescaped markup or null if the input is null
      • firstPathComponent

        public static java.lang.String firstPathComponent​(java.lang.String path,
                                                          char separator)
        Gets the first path component of a path using a given separator. If the separator cannot be found, the path itself is returned.

        For example, firstPathComponent("foo.bar", '.') would return "foo" and firstPathComponent("foo", '.') would return "foo".

        Parameters:
        path - The path to parse
        separator - The path separator character
        Returns:
        The first component in the path or path itself if no separator characters exist.
      • fromEscapedUnicode

        public static java.lang.String fromEscapedUnicode​(java.lang.String escapedUnicodeString)
        Converts encoded \uxxxx to unicode chars and changes special saved chars to their original forms.
        Parameters:
        escapedUnicodeString - escaped unicode string, like '你好'.
        Returns:
        The actual unicode. Can be used for instance with message bundles
      • isEmpty

        public static boolean isEmpty​(java.lang.CharSequence string)
        Checks whether the string is considered empty. Empty means that the string may contain whitespace, but no visible characters. "\n\t " is considered empty, while " a" is not.
        Parameters:
        string - The string
        Returns:
        True if the string is null or ""
      • isEqual

        public static boolean isEqual​(java.lang.String string1,
                                      java.lang.String string2)
        Checks whether two strings are equals taken care of 'null' values and treating 'null' same as trim(string).equals("")
        Parameters:
        string1 -
        string2 -
        Returns:
        true, if both strings are equal
      • isTrue

        public static boolean isTrue​(java.lang.String s)
                              throws StringValueConversionException
        Converts the text in s to a corresponding boolean. On, yes, y, true and 1 are converted to true. Off, no, n, false and 0 (zero) are converted to false. An empty string is converted to false. Conversion is case-insensitive, and does not take internationalization into account. 'Ja', 'Oui', 'Igen', 'Nein', 'Nee', 'Non', 'Nem' are all illegal values.
        Parameters:
        s - the value to convert into a boolean
        Returns:
        Boolean the converted value of s
        Throws:
        StringValueConversionException - when the value of s is not recognized.
      • join

        public static java.lang.String join​(java.lang.String separator,
                                            java.util.List<java.lang.String> fragments)
        Joins string fragments using the specified separator
        Parameters:
        separator -
        fragments -
        Returns:
        combined fragments
      • join

        public static java.lang.String join​(java.lang.String separator,
                                            java.lang.String... fragments)
        Joins string fragments using the specified separator
        Parameters:
        separator -
        fragments -
        Returns:
        combined fragments
      • lastPathComponent

        public static java.lang.String lastPathComponent​(java.lang.String path,
                                                         char separator)
        Gets the last path component of a path using a given separator. If the separator cannot be found, the path itself is returned.

        For example, lastPathComponent("foo.bar", '.') would return "bar" and lastPathComponent("foo", '.') would return "foo".

        Parameters:
        path - The path to parse
        separator - The path separator character
        Returns:
        The last component in the path or path itself if no separator characters exist.
      • replaceAll

        public static java.lang.CharSequence replaceAll​(java.lang.CharSequence s,
                                                        java.lang.CharSequence searchFor,
                                                        java.lang.CharSequence replaceWith)
        Replace all occurrences of one string replaceWith another string.
        Parameters:
        s - The string to process
        searchFor - The value to search for
        replaceWith - The value to searchFor replaceWith
        Returns:
        The resulting string with searchFor replaced with replaceWith
      • replaceHtmlEscapeNumber

        public static java.lang.String replaceHtmlEscapeNumber​(java.lang.String str)
        Replace HTML numbers like &#20540; by the appropriate character.
        Parameters:
        str - The text to be evaluated
        Returns:
        The text with "numbers" replaced
      • split

        public static java.lang.String[] split​(java.lang.String s,
                                               char c)
        Simpler, faster version of String.split() for splitting on a simple character.
        Parameters:
        s - The string to split
        c - The character to split on
        Returns:
        The array of strings
      • stripEnding

        public static java.lang.String stripEnding​(java.lang.String s,
                                                   java.lang.String ending)
        Strips the ending from the string s.
        Parameters:
        s - The string to strip
        ending - The ending to strip off
        Returns:
        The stripped string or the original string if the ending did not exist
      • stripJSessionId

        public static java.lang.String stripJSessionId​(java.lang.String url)
        Strip any jsessionid and possibly other redundant info that might be in our way.
        Parameters:
        url - The url to strip
        Returns:
        The stripped url
      • toChar

        public static char toChar​(java.lang.String s)
                           throws StringValueConversionException
        Converts the 1 character string s to a character.
        Parameters:
        s - The 1 character string to convert to a char.
        Returns:
        Character value to convert
        Throws:
        StringValueConversionException - when the string is longer or shorter than 1 character, or null.
      • toEscapedUnicode

        public static java.lang.String toEscapedUnicode​(java.lang.String unicodeString)
        Converts unicodes to encoded \uxxxx.
        Parameters:
        unicodeString - The unicode string
        Returns:
        The escaped unicode string, like '你好'.
      • toMultilineMarkup

        public static java.lang.CharSequence toMultilineMarkup​(java.lang.CharSequence s)
        Converts a String to multiline HTML markup by replacing newlines with line break entities (<br/>) and multiple occurrences of newline with paragraph break entities (<p>).
        Parameters:
        s - String to transform
        Returns:
        String with all single occurrences of newline replaced with <br/> and all multiple occurrences of newline replaced with <p>.
      • toString

        public static java.lang.String toString​(java.lang.Object object)
        Converts the given object to a string. Does special conversion for throwables and String arrays of length 1 (in which case it just returns to string in that array, as this is a common thing to have in the Servlet API).
        Parameters:
        object - The object
        Returns:
        The string
      • toString

        public static java.lang.String toString​(java.lang.Throwable throwable)
        Converts a Throwable to a string.
        Parameters:
        throwable - The throwable
        Returns:
        The string
      • lengthInBytes

        public static int lengthInBytes​(java.lang.String string,
                                        java.nio.charset.Charset charset)
        Calculates the length of string in bytes, uses specified charset if provided.
        Parameters:
        string -
        charset - (optional) character set to use when converting string to bytes
        Returns:
        length of string in bytes
      • startsWith

        public static boolean startsWith​(java.lang.String str,
                                         java.lang.String prefix,
                                         boolean caseSensitive)
        Extended String.startsWith(String) with support for case sensitivity
        Parameters:
        str -
        prefix -
        caseSensitive -
        Returns:
        true if str starts with prefix
      • indexOf

        public static int indexOf​(java.lang.CharSequence sequence,
                                  char ch)
        returns the zero-based index of a character within a char sequence. this method mainly exists as an faster alternative for sequence.toString().indexOf(ch).
        Parameters:
        sequence - character sequence
        ch - character to search for
        Returns:
        index of character within character sequence or -1 if not found
      • getLevenshteinDistance

        public static int getLevenshteinDistance​(java.lang.CharSequence s,
                                                 java.lang.CharSequence t)

        Find the Levenshtein distance between two Strings.

        This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).

        The previous implementation of the Levenshtein distance algorithm was from http://www.merriampark.com/ld.htm

        Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError which can occur when my Java implementation is used with very large strings.
        This implementation of the Levenshtein distance algorithm is from http://www.merriampark.com/ldjava.htm

         Strings.getLevenshteinDistance(null, *)             = IllegalArgumentException
         Strings.getLevenshteinDistance(*, null)             = IllegalArgumentException
         Strings.getLevenshteinDistance("","")               = 0
         Strings.getLevenshteinDistance("","a")              = 1
         Strings.getLevenshteinDistance("aaapppp", "")       = 7
         Strings.getLevenshteinDistance("frog", "fog")       = 1
         Strings.getLevenshteinDistance("fly", "ant")        = 3
         Strings.getLevenshteinDistance("elephant", "hippo") = 7
         Strings.getLevenshteinDistance("hippo", "elephant") = 7
         Strings.getLevenshteinDistance("hippo", "zzzzzzzz") = 8
         Strings.getLevenshteinDistance("hello", "hallo")    = 1
         
        Copied from Apache commons-lang StringUtils 3.0
        Parameters:
        s - the first String, must not be null
        t - the second String, must not be null
        Returns:
        result distance
        Throws:
        java.lang.IllegalArgumentException - if either String input null
      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)
        convert byte array to hex string
        Parameters:
        bytes - bytes to convert to hexadecimal representation
        Returns:
        hex string
      • toEnum

        public static <T extends java.lang.Enum<T>> T toEnum​(java.lang.CharSequence value,
                                                             java.lang.Class<T> enumClass)
        Return this value as en enum value.
        Parameters:
        value - the value to convert to an enum value
        enumClass - the enum type
        Returns:
        an enum value
      • defaultIfEmpty

        public static java.lang.String defaultIfEmpty​(java.lang.String originalString,
                                                      java.lang.String defaultValue)
        Returns the original string if this one is not empty (i.e. isEmpty(CharSequence) returns false), otherwise the default one is returned. The default string might be itself an empty one.
        Parameters:
        originalString - the original sting value
        defaultValue - the default string to return if the original is empty
        Returns:
        the original string value if not empty, the default one otherwise