|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.wicket.util.string.Strings
public final class Strings
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()).
Field Summary | |
---|---|
static java.lang.String |
LINE_SEPARATOR
The line separator for the current platform. |
Method Summary | |
---|---|
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.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.util.List<java.lang.String> fragments)
Joins string fragments using the specified separator |
static java.lang.String |
join(java.lang.String separator,
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 值 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 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 |
---|
public static final java.lang.String LINE_SEPARATOR
Method Detail |
---|
public static java.lang.String afterFirst(java.lang.String s, char c)
s
- The stringc
- The character
public static java.lang.String afterFirstPathComponent(java.lang.String path, char separator)
For example, afterFirstPathComponent("foo.bar.baz", '.') would return "bar.baz" and afterFirstPathComponent("foo", '.') would return "".
path
- The path to parseseparator
- The path separator character
public static java.lang.String afterLast(java.lang.String s, char c)
s
- The stringc
- The character
public static java.lang.String beforeFirst(java.lang.String s, char c)
s
- The stringc
- The character
public static java.lang.String beforeLast(java.lang.String s, char c)
s
- The stringc
- The character
public static java.lang.String beforeLastPathComponent(java.lang.String path, char separator)
For example, beforeLastPathComponent("foo.bar.baz", '.') would return "foo.bar" and beforeLastPathComponent("foo", '.') would return "".
path
- The path to parseseparator
- The path separator character
public static java.lang.String capitalize(java.lang.String s)
s
- The string
public static java.lang.CharSequence escapeMarkup(java.lang.CharSequence s)
s
- The characters to escape
Strings#escapeMarkup(String, boolean)
public static java.lang.CharSequence escapeMarkup(java.lang.CharSequence s, boolean escapeSpaces)
s
- The characters to escapeescapeSpaces
- True to replace ' ' with nonbreaking space
public static java.lang.CharSequence escapeMarkup(java.lang.CharSequence s, boolean escapeSpaces, boolean convertToHtmlUnicodeEscapes)
s
- The characters to escapeescapeSpaces
- True to replace ' ' with nonbreaking spaceconvertToHtmlUnicodeEscapes
- True to convert non-7 bit characters to unicode HTML (...)
public static java.lang.CharSequence unescapeMarkup(java.lang.String markup)
markup
passed.
markup
- The source String
to unescape.
null if the input is null
public static java.lang.String firstPathComponent(java.lang.String path, char separator)
For example, firstPathComponent("foo.bar", '.') would return "foo" and firstPathComponent("foo", '.') would return "foo".
path
- The path to parseseparator
- The path separator character
public static java.lang.String fromEscapedUnicode(java.lang.String escapedUnicodeString)
escapedUnicodeString
- escaped unicode string, like '你好'.
public static boolean isEmpty(java.lang.CharSequence string)
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.
string
- The string
public static boolean isEqual(java.lang.String string1, java.lang.String string2)
string1
- string2
-
public static boolean isTrue(java.lang.String s) throws StringValueConversionException
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.
s
- the value to convert into a boolean
s
StringValueConversionException
- when the value of s
is not recognized.public static java.lang.String join(java.lang.String separator, java.util.List<java.lang.String> fragments)
separator
- fragments
-
public static java.lang.String join(java.lang.String separator, java.lang.String... fragments)
separator
- fragments
-
public static java.lang.String lastPathComponent(java.lang.String path, char separator)
For example, lastPathComponent("foo.bar", '.') would return "bar" and lastPathComponent("foo", '.') would return "foo".
path
- The path to parseseparator
- The path separator character
public static java.lang.CharSequence replaceAll(java.lang.CharSequence s, java.lang.CharSequence searchFor, java.lang.CharSequence replaceWith)
s
- The string to processsearchFor
- The value to search forreplaceWith
- The value to searchFor replaceWith
public static java.lang.String replaceHtmlEscapeNumber(java.lang.String str)
str
- The text to be evaluated
public static java.lang.String[] split(java.lang.String s, char c)
s
- The string to splitc
- The character to split on
public static java.lang.String stripEnding(java.lang.String s, java.lang.String ending)
s
.
s
- The string to stripending
- The ending to strip off
public static java.lang.String stripJSessionId(java.lang.String url)
url
- The url to strip
public static java.lang.Boolean toBoolean(java.lang.String s) throws StringValueConversionException
isTrue
for valid values of s.
s
- The string to convert.
TRUE
when isTrue(s)
.
StringValueConversionException
- when s is not a valid valueisTrue(String)
public static char toChar(java.lang.String s) throws StringValueConversionException
s
- The 1 character string to convert to a char.
StringValueConversionException
- when the string is longer or shorter than 1 character, or null
.public static java.lang.String toEscapedUnicode(java.lang.String unicodeString)
unicodeString
- The unicode string
public static java.lang.CharSequence toMultilineMarkup(java.lang.CharSequence s)
s
- String to transform
public static java.lang.String toString(java.lang.Object object)
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).
object
- The object
public static java.lang.String toString(java.lang.Throwable throwable)
throwable
- The throwable
public static int lengthInBytes(java.lang.String string, java.nio.charset.Charset charset)
charset
if provided.
string
- charset
- (optional) character set to use when converting string to bytes
public static boolean startsWith(java.lang.String str, java.lang.String prefix, boolean caseSensitive)
String.startsWith(String)
with support for case sensitivity
str
- prefix
- caseSensitive
-
true
if str
starts with prefix
public static int indexOf(java.lang.CharSequence sequence, char ch)
sequence.toString().indexOf(ch)
.
sequence
- character sequencech
- character to search for
-1
if not foundpublic 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
StringUtils.getLevenshteinDistance(null, *) = IllegalArgumentException StringUtils.getLevenshteinDistance(*, null) = IllegalArgumentException StringUtils.getLevenshteinDistance("","") = 0 StringUtils.getLevenshteinDistance("","a") = 1 StringUtils.getLevenshteinDistance("aaapppp", "") = 7 StringUtils.getLevenshteinDistance("frog", "fog") = 1 StringUtils.getLevenshteinDistance("fly", "ant") = 3 StringUtils.getLevenshteinDistance("elephant", "hippo") = 7 StringUtils.getLevenshteinDistance("hippo", "elephant") = 7 StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8 StringUtils.getLevenshteinDistance("hello", "hallo") = 1Copied from Apache commons-lang StringUtils 3.0
s
- the first String, must not be nullt
- the second String, must not be null
java.lang.IllegalArgumentException
- if either String input null
public static java.lang.String toHexString(byte[] bytes)
bytes
- bytes to convert to hexadecimal representation
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |