|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.arsdigita.util.StringUtils
A (static) class of generally-useful string utilities.
Nested Class Summary | |
static interface |
StringUtils.PlaceholderValueGenerator
An interface allowing the value for a placeholder to be dynamically generated. |
Field Summary | |
static String |
NEW_LINE
|
Method Summary | |
static String |
addNewline(String s)
Add a possible newline for proper wrapping. |
static boolean |
emptyString(Object o)
Tests if a string is empty. |
static boolean |
emptyString(String s)
Tests if a string is empty. |
static String |
getParameter(String key,
String plist,
char sep)
Extract a parameter value from a packed list of parameter values. |
static List |
getStackList(Throwable throwable)
Returns a list of lines where each line represents one level in the stack trace captured by throwable . |
static String |
getStackTrace(Throwable throwable)
|
static String |
htmlToText(String s)
Removes tags and substitutes P tags with newlines. |
static String |
interpolate(String text,
Map vars)
This method performs interpolation on multiple variables. |
static String |
interpolate(String text,
String key,
String value)
THis method performs a single variable substitution on a string. |
static boolean |
isAlphaNumeric(String value)
Returns true if the String is AlphaNumeric. |
static String |
join(List elements,
String sep)
"join" a List of Strings into a single string, with each string separated by a defined separator string. |
static String |
join(String[] strings,
char joinChar)
Converts an array of Strings into a single String separated by a given character. |
static String |
join(String[] strings,
String joinStr)
Converts an array of Strings into a single String separated by a given string. |
static String |
nullToEmptyString(String s)
If the String is null, returns an empty string. |
static String |
quoteHtml(String s)
Escapes some "special" characters in HTML text (ampersand, angle brackets, quote). |
static String |
repeat(char pattern,
int repeatCount)
Returns a String containing the specified repeat count of a given pattern character. |
static String |
repeat(String pattern,
int repeatCount)
Returns a String containing the specified repeat count of a given pattern String. |
static String |
replace(String str,
String find,
String replace)
Finds all occurrences of find in str and
replaces them with them with replace . |
static String |
smartTextToHtml(String s)
Converts plain text with simple inline markup into HTML. |
static String[] |
split(String s,
char sep)
Convert a string of items separated by a separator character to an array of the items. |
static List |
splitUp(String s,
String re)
Given a string, split it into substrings matching a regular expression that you supply. |
static String |
stripNewLines(String str)
Strips all new-line characters from the input string. |
static String |
stripWhiteSpace(String s)
Strip extra white space from a string. |
static String |
textToHtml(String s)
Takes a plaintext string, and returns an HTML string that, when rendered by a web browser, will appear as the original input string |
static String |
toString(Map m)
create a String representation of a map. |
static String |
toString(Object o)
Get a String representation for an Object. |
static String |
trimleft(String s)
Removes whitespace from the beginning of a string. |
static String |
truncateString(String s,
int length)
This takes the passed in string and truncates it. |
static String |
truncateString(String s,
int length,
boolean removeHTML)
This takes the passed in string and truncates it. |
static String |
wrap(String input)
Wrap a string to be no wider than 80 characters. |
static String |
wrap(String input,
int maxLength)
Wrap a string to be no wider than a specified number of characters by inserting line breaks. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String NEW_LINE
Method Detail |
public static final boolean emptyString(String s)
s
- A string to test
true
if s
is null or empty;
otherwise false
public static final boolean emptyString(Object o)
o
- A string to test
true
if o
is null or empty;
otherwise false
public static final String nullToEmptyString(String s)
public static final String quoteHtml(String s)
s
- The plain-text string to quote
public static final String textToHtml(String s)
s
- The input plaintext string
<p>and ampersands/angle brackets escaped.
public static final String htmlToText(String s)
HtmlToText
.
public static final String smartTextToHtml(String s)
text
* my item
* my next item
* my final item
+ my item
+ my next item
+ my final item
public static String[] split(String s, char sep)
s
- string contains items separated by a separator character.sep
- separator character.
public static List splitUp(String s, String re)
Given a string, split it into substrings matching a regular expression that you supply. Parts of the original string which don't match the regular expression also appear as substrings. The upshot of this is that the final substrings can be concatenated to get the original string.
As an example, let's say the original string is:
s = "/packages/foo/xsl/::vhost::/foo_::locale::.xsl";
We call the function like this:
output = splitUp (s, "/::\\w+::/");
The result (output
) will be the following list:
("/packages/foo/xsl/", "::vhost::", "/foo_", "::locale::", ".xsl")
Notice the important property that concatenating all these strings together will restore the original string.
Here is another useful example. To split up HTML into elements and content, do:
output = splitUp (html, "/<.*?>/");
You will end up with something like this:
("The following text will be ", "", "bold", "", ".")
s
- The original string to split.re
- The regular expression in the format required by
Perl5Util.match(String, String)
.
public static String join(String[] strings, char joinChar)
strings
- The string array too join.joinChar
- The character to join the array members together.
public static String join(String[] strings, String joinStr)
strings
- The string array too join.joinStr
- The string to join the array members together.
public static String getParameter(String key, String plist, char sep)
key
- the key indicating which parameter value to extract.plist
- packed list of key=value assignments. The character '='
must be used to indicate the assignment.sep
- separator character.
public static String stripWhiteSpace(String s)
s
- string that may contain extra white space
public static String toString(Object o)
public static String toString(Map m)
public static String stripNewLines(String str)
str
- a string to strip
public static String addNewline(String s)
Add a possible newline for proper wrapping.
Checks the given String to see if it ends with whitspace. If so, it
assumes this whitespace is intentional formatting and returns a reference
to the original string. If not, a new String
object is
created containing the original plus a platform-dependent newline
character obtained from System.getProperty("line.separator")
.
public static String truncateString(String s, int length)
public static String truncateString(String s, int length, boolean removeHTML)
s
- The string to be truncatedlength
- The length which to truncate the stringremoveHTML
- Whether or not to convert the HTML to textpublic static String join(List elements, String sep)
elements
- the strings to join togethersep
- the separator string
public static final String trimleft(String s)
public static final String repeat(String pattern, int repeatCount)
pattern
- the pattern StringrepeatCount
- the number of time to repeat itpublic static final String repeat(char pattern, int repeatCount)
pattern
- the pattern characterrepeatCount
- the number of time to repeat itpublic static final String wrap(String input)
input
- the String to wrappublic static final String wrap(String input, int maxLength)
Notes:
input
- the String to wrapmaxLength
- the maximum number of characters between line
breakspublic static boolean isAlphaNumeric(String value)
value
- String to check
public static String interpolate(String text, Map vars)
text
- the text to interpolatevars
- a hash table containing key -> value mappingsMessageFormat
public static String interpolate(String text, String key, String value)
text
- the text to process for substitutionskey
- the name of the placeholdervalue
- the value to insert upon encountering a placeholderMessageFormat
public static String replace(String str, String find, String replace)
find
in str
and
replaces them with them with replace
.
public static String getStackTrace(Throwable throwable)
NullPointerException
- if throwable
is nullpublic static List getStackList(Throwable throwable)
throwable
.
For a stack trace like this:
java.lang.Throwable at Main.level3(Main.java:19) at Main.level2(Main.java:15) at Main.level1(Main.java:11) at Main.main(Main.java:7)
the returned list looks like this:
["java.lang.Throwable", "Main.level3(Main.java:20)", "Main.level2(Main.java:15)", "Main.level1(Main.java:11)", "Main.main(Main.java:7)"]
NullPointerException
- if throwable
is nullgetStackTrace(Throwable)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |