|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.opensymphony.util.TextUtils
public class TextUtils
Utilities for common String manipulations. This is a class contains static methods only and is not meant to be instantiated.
Field Summary | |
---|---|
static String[] |
SINGLE_TAGS
An array of HTML tags that, in HTML, don't require closing tags. |
Constructor Summary | |
---|---|
TextUtils()
|
Method Summary | |
---|---|
static String |
br(String s)
Convert line breaks to html <br> tag. |
static String |
closeTags(String str)
Search through a String for any tags that have been opened and append closing tags for those that have not been closed. |
static String |
colorToHex(Color c)
Convert Color to html hex string. |
static byte[] |
decodeBytes(String str)
Decode binary data from String using base64. |
static Object |
decodeObject(String str)
Decode Object from a String by decoding with base64 then deserializing. |
static String |
encodeBytes(byte[] data)
Encode binary data into String using base64. |
static String |
encodeObject(Object o)
Encode an Object to String by serializing it and encoding using base64. |
static String |
extractNumber(String in)
Extract a number from a String. |
static Color |
hexToColor(String color)
Convert html hex string to Color. |
static String |
html(String s)
Deprecated. use htmlEncode(String) instead. |
static String |
htmlEncode(String s)
|
static String |
htmlEncode(String s,
boolean encodeSpecialChars)
Escape html entity characters and high characters (eg "curvy" Word quotes). |
static String |
hyperlink(String text)
Convert all URLs and E-mail addresses in a string into hyperlinks. |
static String |
hyperlink(String text,
String target)
Convert all URLs and E-mail addresses in a string into hyperlinks. |
static String |
indent(String string,
int indentSize,
boolean initialLine)
Indent a String with line-breaks. |
static String |
innerTrim(String s)
Returns a string that has whitespace removed from both ends of the String, as well as duplicate whitespace removed from within the String. |
static String |
join(String glue,
Collection pieces)
Join a Collection of Strings together. |
static String |
join(String glue,
Iterator pieces)
Join an Iteration of Strings together. |
static String |
join(String glue,
String[] pieces)
Join an array of Strings together. |
static String |
leadingSpaces(String s)
Finds all leading spaces on each line and replaces it with an HTML space ( ) |
static String |
left(String s,
int n)
Returns the leftmost n chars of the string. |
static String |
linkEmail(String str)
Wrap all email addresses in specified string with href tags. |
static String |
linkURL(String str)
Wrap all urls ('http://', 'www.', and 'ftp://') in specified string with href tags. |
static String |
linkURL(String str,
String target)
Wrap all urls ('abc://' and 'www.abc') in specified string with href tags. |
static String |
list(String str)
Create elements in a piece of plain text; Will convert lines starting with - or *. |
static String |
noNull(String string)
Return string , or "" if string
is null . |
static String |
noNull(String string,
String defaultString)
Return string , or defaultString if
string is null or "" . |
static boolean |
parseBoolean(String in)
Convert a String to an boolean. |
static Date |
parseDate(String year,
String month,
String day)
Given 3 Strings representing the the year, month and day, return a Date object. |
static double |
parseDouble(String in)
Convert a String to a double. |
static float |
parseFloat(String in)
Convert a String to a float. |
static int |
parseInt(String in)
Convert a String to an int. |
static long |
parseLong(String in)
Convert a String to a long. |
static String |
plainTextToHtml(String str)
Converts plain text to html code. |
static String |
plainTextToHtml(String str,
boolean encodeSpecialChars)
|
static String |
plainTextToHtml(String str,
String target)
|
static String |
plainTextToHtml(String str,
String target,
boolean encodeSpecialChars)
Converts plain text to html code. |
static String |
removeAndInsert(String str,
int removeAndInsertStart,
int removeEnd,
String insertStr)
Removes part of the original string starting at removeAndInsertStart and ending at removeEnd. |
static String |
slashes(String s)
Escape chars that need slashes in front of them. |
static boolean |
stringSet(String string)
Check whether string has been set to
something other than "" or null . |
static String |
trimToEndingChar(String str,
int len)
Trim a String to the specified length. |
static boolean |
verifyEmail(String email)
Verify that the given string is a valid email address. |
static boolean |
verifyUrl(String url)
Verify That the given String is in valid URL format. |
static String |
wrapParagraph(String s)
Wrap paragraphs in html <p> tags. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String[] SINGLE_TAGS
Constructor Detail |
---|
public TextUtils()
Method Detail |
---|
public static final String br(String s)
<br>
tag.
s
- the String to convert
public static final String closeTags(String str)
str
- A string possibly containing unclosed HTML tags
public static final String colorToHex(Color c)
c
- the Color to convert
public static final byte[] decodeBytes(String str) throws IOException
IOException
encodeBytes(byte[])
public static final Object decodeObject(String str) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
encodeObject(java.lang.Object)
public static final String encodeBytes(byte[] data) throws IOException
IOException
decodeBytes(java.lang.String)
public static final String encodeObject(Object o) throws IOException
IOException
decodeObject(java.lang.String)
public static final String extractNumber(String in)
" 12345" -> "12345" "hello123bye" -> "123" "a2b4c6 8 " -> "2468" " -22" -> "-22" "5.512" -> "5.512" "1.2.3.4" -> "1.234" ".2" -> "0.2" "-555.7" -> "-555.7" "-..6" -> "-0.6" "abc- dx.97 9" -> "-0.979" "?1,000,000.00 per year" -> "1000000.00" "" -> "0" "asdsf" -> "0" "123." -> "123" null -> "0"
in
- Original String containing number to be extracted.
parseInt(String)
,
parseLong(String)
public static final Color hexToColor(String color)
Color.black
is returned.
Only the first six hexadecimal characters are considered; any
extraneous values are discarded. Also, a leading "#", if any, is allowed
(and ignored).
color
- the String (in RGB hexadecimal format) to convert
public static final String html(String s)
public static final String htmlEncode(String s)
public static final String htmlEncode(String s, boolean encodeSpecialChars)
s
- the String to escape.encodeSpecialChars
- if true high characters will be encode other wise not.
public static final String hyperlink(String text)
text
- The block of text to hyperlink.
hyperlink(String, String)
public static final String hyperlink(String text, String target)
text
- The block of text to hyperlink.target
- The target attribute to use for href (optional).
linkEmail(String)
,
linkURL(String)
public static final String indent(String string, int indentSize, boolean initialLine)
string
- String to indent.indentSize
- Number of spaces to indent by. 0 will indent using a tab.initialLine
- Whether to indent initial line.
public static final String innerTrim(String s)
public static final String join(String glue, Iterator pieces)
// get Iterator of Strings ("abc","def","123"); Iterator i = getIterator(); out.print( TextUtils.join(", ",i) ); // prints: "abc, def, 123"
glue
- Token to place between Strings.pieces
- Iteration of Strings to join.
public static final String join(String glue, String[] pieces)
glue
- Token to place between Strings.pieces
- Array of Strings to join.
join(String, java.util.Iterator)
public static final String join(String glue, Collection pieces)
glue
- Token to place between Strings.pieces
- Collection of Strings to join.
join(String, java.util.Iterator)
public static final String leadingSpaces(String s)
s
- string containing text to replaced with
public static final String left(String s, int n)
s
- - the string to operate on.n
- - the number of chars to return.public static final String linkEmail(String str)
str
- The block of text to check.
public static final String linkURL(String str)
str
- The block of text to check.
public static final String linkURL(String str, String target)
str
- The block of text to check.target
- The target to use for the href (optional).
public static final String list(String str)
str
- A string, possibly containing a plaintext "list"
public static final String noNull(String string, String defaultString)
string
, or defaultString
if
string
is null
or ""
.
Never returns null
.
Examples:
// prints "hello" String s=null; System.out.println(TextUtils.noNull(s,"hello"); // prints "hello" s=""; System.out.println(TextUtils.noNull(s,"hello"); // prints "world" s="world"; System.out.println(TextUtils.noNull(s, "hello");
string
- the String to check.defaultString
- The default string to return if string
is null
or ""
string
if string
is non-empty, and defaultString
otherwisestringSet(java.lang.String)
public static final String noNull(String string)
string
, or ""
if string
is null
. Never returns null
.
Examples:
// prints 0 String s=null; System.out.println(TextUtils.noNull(s).length()); // prints 1 s="a"; System.out.println(TextUtils.noNull(s).length());
string
- the String to check
public static final boolean parseBoolean(String in)
false
is returned.
in
- String to be parsed.
public static final Date parseDate(String year, String month, String day)
If the day cannot be determined, 1st will be used. If the month cannot be determined, Jan will be used.
year
- Year : 4 digitmonth
- Month : 1 or 2 digit (1=jan, 2=feb, ...) or name (jan, JAN, January, etc). If null, default is Jan.day
- Day of month : 1 or 2 digit, prefix will be stripped (1, 30, 05, 3rd). If null, default is 1st.public static final double parseDouble(String in)
in
- String containing number to be parsed.
extractNumber(String)
public static final float parseFloat(String in)
in
- String containing number to be parsed.
extractNumber(String)
public static final int parseInt(String in)
in
- String containing number to be parsed.
extractNumber(String)
public static final long parseLong(String in)
in
- String containing number to be parsed.
extractNumber(String)
public static final String plainTextToHtml(String str)
str
- - String containing the plain text.
public static final String plainTextToHtml(String str, boolean encodeSpecialChars)
public static final String plainTextToHtml(String str, String target)
public static final String plainTextToHtml(String str, String target, boolean encodeSpecialChars)
str
- - String containing the plain text.target
- - Target for href tags (optional).encodeSpecialChars
- - if true high characters will be encode other wise not.
public static final String removeAndInsert(String str, int removeAndInsertStart, int removeEnd, String insertStr)
str
- - the original string.removeAndInsertStart
- - the index within the original string at which removal is to start.removeEnd
- - the index within the original string at which removal is to end (exclusive - ie. does not
remove the character at that index).insertStr
- - the string to be inserted.public static final String slashes(String s)
s
- the String to add escape characters to
public static final boolean stringSet(String string)
string
has been set to
something other than ""
or null
.
string
- the String
to check
public static final String trimToEndingChar(String str, int len)
str
- - String to trim.len
- - length to which string is to be trimmed.
public static final boolean verifyEmail(String email)
email
- The email address to verify.
public static final boolean verifyUrl(String url)
url
- The url string to verify.
public static final String wrapParagraph(String s)
<p>
tags.
Paragraphs are seperated by blank lines.
s
- the String to reformat
|
OSCore Project Page | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |