org.apache.wicket.util.string
Class Strings

java.lang.Object
  extended by 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 Summary
static String LINE_SEPARATOR
          The line separator for the current platform.
 
Method Summary
static String afterFirst(String s, char c)
          Returns everything after the first occurrence of the given character in s.
static String afterFirstPathComponent(String path, char separator)
          Gets everything after the first path component of a path using a given separator.
static String afterLast(String s, char c)
          Returns everything after the last occurrence of the given character in s.
static String beforeFirst(String s, char c)
          Returns everything before the first occurrence of the given character in s.
static String beforeLast(String s, char c)
          Returns everything before the last occurrence of the given character in s.
static String beforeLastPathComponent(String path, char separator)
          Gets everything before the last path component of a path using a given separator.
static String capitalize(String s)
          Capitalizes a string.
static CharSequence escapeMarkup(String s)
          Converts a Java String to an HTML markup string, but does not convert normal spaces to non-breaking space entities (<nbsp>).
static CharSequence escapeMarkup(String s, boolean escapeSpaces)
          Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate.
static CharSequence escapeMarkup(String s, boolean escapeSpaces, boolean convertToHtmlUnicodeEscapes)
          Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate.
static String firstPathComponent(String path, char separator)
          Gets the first path component of a path using a given separator.
static String fromEscapedUnicode(String escapedUnicodeString)
          Converts encoded \uxxxx to unicode chars and changes special saved chars to their original forms.
static boolean isEmpty(CharSequence string)
          Checks whether the string is considered empty.
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("")
static boolean isTrue(String s)
          Converts the text in s to a corresponding boolean.
static String join(String separator, String... fragments)
          Joins string fragments using the specified separator
static String lastPathComponent(String path, char separator)
          Gets the last path component of a path using a given separator.
static int lengthInBytes(String string, Charset charset)
          Calculates the length of string in bytes, uses specified charset if provided.
static CharSequence replaceAll(CharSequence s, CharSequence searchFor, CharSequence replaceWith)
          Replace all occurrences of one string replaceWith another string.
static String replaceHtmlEscapeNumber(String str)
          Replace HTML numbers like 值 by the appropriate character.
static String[] split(String s, char c)
          Simpler, faster version of String.split() for splitting on a simple character.
static boolean startsWith(String str, String prefix, boolean caseSensitive)
          Extended String.startsWith(String) with support for case sensitivity
static String stripEnding(String s, String ending)
          Strips the ending from the string s.
static String stripJSessionId(CharSequence url)
          Strip any jsessionid and possibly other redundant info that might be in our way.
static Boolean toBoolean(String s)
          Converts the string s to a Boolean.
static char toChar(String s)
          Converts the 1 character string s to a character.
static String toEscapedUnicode(String unicodeString)
          Converts unicodes to encoded \uxxxx.
static CharSequence toMultilineMarkup(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 String toString(Component component, Throwable location)
          Creates a location stacktrace string representation for the component for reference when the render check fails.
static String toString(Object object)
          Converts the given object to a string.
static String toString(Throwable throwable)
          Converts a Throwable to a string.
 
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 String LINE_SEPARATOR
The line separator for the current platform.

Method Detail

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

public static CharSequence escapeMarkup(String 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 string to escape
Returns:
The escaped string
See Also:
escapeMarkup(String, boolean)

escapeMarkup

public static CharSequence escapeMarkup(String 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 string to escape
escapeSpaces - True to replace ' ' with nonbreaking space
Returns:
The escaped string

escapeMarkup

public static CharSequence escapeMarkup(String 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 string to escape
escapeSpaces - True to replace ' ' with nonbreaking space
convertToHtmlUnicodeEscapes - True to convert non-7 bit characters to unicode HTML (&#...)
Returns:
The escaped string

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 ""

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,
                          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 值 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(CharSequence 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

public static Boolean toBoolean(String s)
                         throws StringValueConversionException
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:
isTrue(String)

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

public static CharSequence toMultilineMarkup(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 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(Component component,
                              Throwable location)
Creates a location stacktrace string representation for the component for reference when the render check fails. This method filters out most of the unnecessary parts of the stack trace. The message of the location is used as a verb in the rendered string. Use "added", "constructed" or similar verbs as values.

Parameters:
component - the component that was constructed or added and failed to render
location - the location where the component was created or added in the java code.
Returns:
a string giving the line precise location where the component was added or created.

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


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.