Class Strings
- java.lang.Object
-
- org.apache.wicket.util.string.Strings
-
public final class Strings extends java.lang.ObjectA 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.StringLINE_SEPARATORThe line separator for the current platform.static java.lang.StringSESSION_ID_PARAM_NAMEThe 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.StringafterFirst(java.lang.String s, char c)Returns everything after the first occurrence of the given character in s.static java.lang.StringafterFirstPathComponent(java.lang.String path, char separator)Gets everything after the first path component of a path using a given separator.static java.lang.StringafterLast(java.lang.String s, char c)Returns everything after the last occurrence of the given character in s.static java.lang.StringbeforeFirst(java.lang.String s, char c)Returns everything before the first occurrence of the given character in s.static java.lang.StringbeforeLast(java.lang.String s, char c)Returns everything before the last occurrence of the given character in s.static java.lang.StringbeforeLastPathComponent(java.lang.String path, char separator)Gets everything before the last path component of a path using a given separator.static java.lang.Stringcapitalize(java.lang.String s)Capitalizes a string.static java.lang.StringdefaultIfEmpty(java.lang.String originalString, java.lang.String defaultValue)Returns the original string if this one is not empty (i.e.static java.lang.CharSequenceescapeMarkup(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.CharSequenceescapeMarkup(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.CharSequenceescapeMarkup(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.StringfirstPathComponent(java.lang.String path, char separator)Gets the first path component of a path using a given separator.static java.lang.StringfromEscapedUnicode(java.lang.String escapedUnicodeString)Converts encoded \uxxxx to unicode chars and changes special saved chars to their original forms.static intgetLevenshteinDistance(java.lang.CharSequence s, java.lang.CharSequence t)Find the Levenshtein distance between two Strings.static intindexOf(java.lang.CharSequence sequence, char ch)returns the zero-based index of a character within a char sequence.static booleanisEmpty(java.lang.CharSequence string)Checks whether thestringis considered empty.static booleanisEqual(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 booleanisTrue(java.lang.String s)Converts the text insto a corresponding boolean.static java.lang.Stringjoin(java.lang.String separator, java.lang.String... fragments)Joins string fragments using the specified separatorstatic java.lang.Stringjoin(java.lang.String separator, java.util.List<java.lang.String> fragments)Joins string fragments using the specified separatorstatic java.lang.StringlastPathComponent(java.lang.String path, char separator)Gets the last path component of a path using a given separator.static intlengthInBytes(java.lang.String string, java.nio.charset.Charset charset)Calculates the length of string in bytes, uses specifiedcharsetif provided.static java.lang.CharSequencereplaceAll(java.lang.CharSequence s, java.lang.CharSequence searchFor, java.lang.CharSequence replaceWith)Replace all occurrences of one string replaceWith another string.static java.lang.StringreplaceHtmlEscapeNumber(java.lang.String str)Replace HTML numbers like 值 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 booleanstartsWith(java.lang.String str, java.lang.String prefix, boolean caseSensitive)ExtendedString.startsWith(String)with support for case sensitivitystatic java.lang.StringstripEnding(java.lang.String s, java.lang.String ending)Strips the ending from the strings.static java.lang.StringstripJSessionId(java.lang.String url)Strip any jsessionid and possibly other redundant info that might be in our way.static java.lang.BooleantoBoolean(java.lang.String s)Converts the string s to a Boolean.static chartoChar(java.lang.String s)Converts the 1 character string s to a character.static <T extends java.lang.Enum<T>>
TtoEnum(java.lang.CharSequence value, java.lang.Class<T> enumClass)Return this value as en enum value.static java.lang.StringtoEscapedUnicode(java.lang.String unicodeString)Converts unicodes to encoded \uxxxx.static java.lang.StringtoHexString(byte[] bytes)convert byte array to hex stringstatic java.lang.CharSequencetoMultilineMarkup(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.StringtoString(java.lang.Object object)Converts the given object to a string.static java.lang.StringtoString(java.lang.Throwable throwable)Converts a Throwable to a string.static java.lang.CharSequenceunescapeMarkup(java.lang.String markup)Unescapes the escaped entities in themarkuppassed.
-
-
-
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 stringc- 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 parseseparator- 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 stringc- 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 stringc- 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 stringc- 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 parseseparator- 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 < entities and greater than signs to > entities.- Parameters:
s- The characters to escapeescapeSpaces- 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 < entities and greater than signs to > entities.- Parameters:
s- The characters to escapeescapeSpaces- True to replace ' ' with nonbreaking spaceconvertToHtmlUnicodeEscapes- 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 themarkuppassed.- Parameters:
markup- The sourceStringto unescape.- Returns:
- the unescaped markup or
null if the input isnull
-
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 parseseparator- 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 thestringis 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 StringValueConversionExceptionConverts the text insto a corresponding boolean. On, yes, y, true and 1 are converted totrue. Off, no, n, false and 0 (zero) are converted tofalse. An empty string is converted tofalse. 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 ofsis 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 parseseparator- 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 processsearchFor- The value to search forreplaceWith- 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 值 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 splitc- 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 strings.- Parameters:
s- The string to stripending- 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
-
toBoolean
public static java.lang.Boolean toBoolean(java.lang.String s) throws StringValueConversionExceptionConverts the string s to a Boolean. SeeisTruefor valid values of s.- Parameters:
s- The string to convert.- Returns:
- Boolean
TRUEwhenisTrue(s). - Throws:
StringValueConversionException- when s is not a valid value- See Also:
isTrue(String)
-
toChar
public static char toChar(java.lang.String s) throws StringValueConversionExceptionConverts 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, ornull.
-
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 forthrowablesand 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 specifiedcharsetif 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)ExtendedString.startsWith(String)with support for case sensitivity- Parameters:
str-prefix-caseSensitive-- Returns:
trueifstrstarts withprefix
-
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 forsequence.toString().indexOf(ch).- Parameters:
sequence- character sequencech- character to search for- Returns:
- index of character within character sequence or
-1if 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.htmStrings.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") = 1Copied from Apache commons-lang StringUtils 3.0- Parameters:
s- the first String, must not be nullt- the second String, must not be null- Returns:
- result distance
- Throws:
java.lang.IllegalArgumentException- if either String inputnull
-
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 valueenumClass- 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 valuedefaultValue- the default string to return if the original is empty- Returns:
- the original string value if not empty, the default one otherwise
-
-