Class Strings

java.lang.Object
org.apache.wicket.util.string.Strings

public final class Strings extends 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 Details

    • SESSION_ID_PARAM_NAME

      public static final 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 Details

    • afterFirst

      public static String afterFirst(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 String afterFirstPathComponent(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 String afterLast(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 String beforeFirst(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 String beforeLast(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 String beforeLastPathComponent(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 String capitalize(String s)
      Capitalizes a string.
      Parameters:
      s - The string
      Returns:
      The capitalized string
    • escapeMarkup

      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

      public static CharSequence escapeMarkup(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 CharSequence escapeMarkup(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 CharSequence unescapeMarkup(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 String firstPathComponent(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 String fromEscapedUnicode(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(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 ""
    • isEmpty

      public static boolean isEmpty(String 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. Note: This method overloads isEmpty(CharSequence) for performance reasons.
      Parameters:
      string - The string
      Returns:
      True if the string is null or ""
    • isEqual

      public static boolean isEqual(String string1, 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(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 String join(String separator, List<String> fragments)
      Joins string fragments using the specified separator
      Parameters:
      separator -
      fragments -
      Returns:
      combined fragments
    • join

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

      public static String lastPathComponent(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 CharSequence replaceAll(CharSequence s, CharSequence searchFor, 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 String replaceHtmlEscapeNumber(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 String[] split(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 String stripEnding(String s, 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 String stripJSessionId(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
    • toBoolean

      Converts the string s to a Boolean. See isTrue for valid values of s.
      Parameters:
      s - The string to convert.
      Returns:
      Boolean TRUE when isTrue(s).
      Throws:
      StringValueConversionException - when s is not a valid value
      See Also:
    • toChar

      public static char toChar(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 String toEscapedUnicode(String unicodeString)
      Converts unicodes to encoded \uxxxx.
      Parameters:
      unicodeString - The unicode string
      Returns:
      The escaped unicode string, like '你好'.
    • toMultilineMarkup

      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 String toString(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 String toString(Throwable throwable)
      Converts a Throwable to a string.
      Parameters:
      throwable - The throwable
      Returns:
      The string
    • lengthInBytes

      public static int lengthInBytes(String string, 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(String str, 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(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

      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:
      IllegalArgumentException - if either String input null
    • toHexString

      public static 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 Enum<T>> T toEnum(CharSequence value, 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 String defaultIfEmpty(String originalString, 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