package org.apache.wicket.util.string;
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()).
The line separator for the current platform.
private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
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.
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
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.
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.
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.
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
Capitalizes a string.
- Parameters:
s
The string- Returns:
- The capitalized string
Converts a Java String to an HTML markup string, but does not convert normal spaces to
non-breaking space entities (<nbsp>).
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 string to escapeescapeSpaces
True to replace ' ' with nonbreaking space- Returns:
- The escaped string
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 string to escapeescapeSpaces
True to replace ' ' with nonbreaking spaceconvertToHtmlUnicodeEscapes
True to convert non-7 bit characters to unicode HTML (&#...)- Returns:
- The escaped string
final boolean convertToHtmlUnicodeEscapes)
for (int i = 0; i < len; i++)
buffer.append(" "); if ((i < len - 1) && (s.charAt(i + 1) == '#'))
if (convertToHtmlUnicodeEscapes)
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.
final int index = path.indexOf(separator);
Converts encoded \uxxxx to unicode chars and changes special saved chars to their
original forms.
- Parameters:
escapedUnicodeString
escaped unicode string, like '\u4F60\u597D'.- Returns:
- The actual unicode. Can be used for instance with message bundles
char[] convtBuf = new char[len];
if (convtBuf.length < len)
convtBuf = new char[newLen];
for (int i = 0; i < 4; i++)
value = (value << 4) + aChar - '0';
value = (value << 4) + 10 + aChar - 'a';
value = (value << 4) + 10 + aChar - 'A';
out[outLen++] = (char)value;
return new String(out, 0, outLen);
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 ""
Checks whether two strings are equals taken care of 'null' values and treating 'null' same as
trim(string).equals("")
- Parameters:
string1string2- Returns:
- true, if both strings are equal
if ((string1 == null) && (string2 == null))
if (string1 == null || string2 == null)
return string1.equals(string2);
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.
Joins string fragments using the specified separator
- Parameters:
separatorfragments- Returns:
- combined fragments
if (fragments.length < 1)
else if (fragments.length < 2)
if (fragments[0] != null)
for (int i = 1; i < fragments.length; i++)
if ((fragments[i - 1] != null) || (fragments[i] != null))
boolean lhsClosed = fragments[i - 1].endsWith(separator);
boolean rhsClosed = fragments[i].startsWith(separator);
if (lhsClosed && rhsClosed)
else if (!lhsClosed && !rhsClosed)
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.
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
if (searchFor == null || "".equals(searchFor))
int matchIndex = search(s, searchString, 0);
final int replaceWithLength = replaceWith.length();
final int searchForLength = searchFor.length();
if (replaceWithLength > searchForLength)
size += (replaceWithLength - searchForLength);
append(buffer, s, pos, matchIndex);
pos = matchIndex + searchForLength;
matchIndex = search(s, searchString, pos);
while (matchIndex != -1);
Replace HTML numbers like 值 by the appropriate character.
- Parameters:
str
The text to be evaluated- Returns:
- The text with "numbers" replaced
int pos = matcher.start();
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
Strips the ending from the string
s.
- 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
if (ending == null || "".equals(ending))
final int endingLength = ending.length();
final int sLength = s.length();
if (endingLength > sLength)
final int endpos = sLength - endingLength;
Strip any jsessionid and possibly other redundant info that might be in our way.
- Parameters:
url
The url to strip- Returns:
- The stripped url
int ixSemiColon = path.indexOf(";"); if (ixEnd <= ixSemiColon)
path.delete(ixSemiColon, ixEnd);
Converts the string s to a Boolean. See
isTrue for valid values of s.
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.
Converts unicodes to encoded \uxxxx.
- Parameters:
unicodeString
The unicode string- Returns:
- The escaped unicode string, like '\u4F60\u597D'.
if ((unicodeString == null) || (unicodeString.length() == 0))
int len = unicodeString.length();
for (int x = 0; x < len; x++)
char aChar = unicodeString.charAt(x);
if ((aChar > 61) && (aChar < 127))
if ((aChar < 0x0020) || (aChar > 0x007e))
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>.
for (int i = 0; i < s.length(); i++)
else if (newlineCount > 1)
else if (newlineCount > 1)
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
if (object instanceof String[] && ((String[])object).length == 1)
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 renderlocation
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.
if (componentType.indexOf('$') >= 0) " with id '" + component.getId() + "' that failed to render was " +
String[] skippedElements = new String[] { "org.apache.wicket.MarkupContainer", "org.apache.wicket.Component", "org.apache.wicket.markup" };
String[] breakingElements = new String[] { "org.apache.wicket.protocol.http.WicketServlet", "org.apache.wicket.protocol.http.WicketFilter", "java.lang.reflect" };
for (int i = 0; i < trace.length; i++)
if (!(traceString.startsWith("sun.reflect.") && i > 1)) if (traceString.indexOf("java.lang.reflect") < 0) for (int i = 0; i < filters.length; i++)
Converts a Throwable to a string.
- Parameters:
throwable
The throwable- Returns:
- The string
int length = al.size() - 1;
sb.append("\n\nComplete stack:\n\n"); for (int i = 0; i < length; i++)
return "<Null Throwable>";
Outputs the throwable and its stacktrace to the stringbuffer. If stopAtWicketSerlvet is true
then the output will stop when the org.apache.wicket servlet is reached. sun.reflect.
packages are filtered out.
- Parameters:
causesbstopAtWicketServlet
boolean stopAtWicketServlet)
for (int i = 0; i < trace.length; i++)
if (!(traceString.startsWith("sun.reflect.") && i > 1)) if (stopAtWicketServlet &&
(traceString.startsWith("org.apache.wicket.protocol.http.WicketServlet") || traceString.startsWith("org.apache.wicket.protocol.http.WicketFilter"))) Convert a nibble to a hex character
- Parameters:
nibble
the nibble to convert.- Returns:
- hex character
private static char toHex(int nibble)
Calculates the length of string in bytes, uses specified
charset if provided.
- Parameters:
stringcharset
(optional) character set to use when converting string to bytes- Returns:
- length of string in bytes
"StringResourceStream created with unsupported charset: " + charset.name());
Private constructor prevents construction.