public class StringUtils extends Object
Modifier and Type | Method and Description |
---|---|
static String |
abbreviate(String str,
int maxWidth)
Abbreviates a string to a specified length and then adds an ellipsis
if the input is greater than the maxWidth.
|
static String |
chopAtWord(String string,
int length)
Intelligently chops a String at a word boundary (whitespace) that occurs
at the specified index in the argument or before.
|
static String |
collectionToString(Collection<String> collection)
Returns a collection of Strings as a comma-delimitted list of strings.
|
static boolean |
contains(String[] array,
String item)
Returns true if the given string is in the given array.
|
static String |
dateToMillis(Date date)
Formats a Date as a fifteen character long String made up of the Date's
padded millisecond value.
|
static byte[] |
decodeBase32(String data)
Decodes a base32 String using the base32hex profile.
|
static byte[] |
decodeBase64(String data)
Decodes a base64 String.
|
static byte[] |
decodeHex(String hex)
Turns a hex encoded string into a byte array.
|
static String |
encodeBase32(byte[] data)
Encodes a byte array into a base32 String using the base32hex profile.
|
static String |
encodeBase32(String data)
Encodes a String as a base32 String using the base32hex profile.
|
static String |
encodeBase64(byte[] data)
Encodes a byte array into a base64 String.
|
static String |
encodeBase64(String data)
Encodes a String as a base64 String.
|
static String |
encodeHex(byte[] bytes)
Turns an array of bytes into a String representing each byte as an
unsigned hex number.
|
static String |
escapeForSQL(String string)
Escapes all necessary characters in the String so that it can be used in SQL
|
static String |
escapeForXML(String string)
Escapes all necessary characters in the String so that it can be used
in an XML doc.
|
static String |
escapeHTMLTags(String in)
This method takes a string which may contain HTML tags (ie, <b>,
<table>, etc) and converts the '<' and '>' characters to
their HTML escape sequences.
|
static String |
escapeHTMLTags(String in,
boolean includeLF)
This method takes a string which may contain HTML tags (ie, <b>,
<table>, etc) and converts the '<' and '>' characters to
their HTML escape sequences.
|
static byte[] |
getBytes(String input)
Returns the UTF-8 bytes for the given String, suppressing
UnsupportedEncodingException (in lieu of log message)
|
static String |
getElapsedTime(long delta)
Returns a textual representation for the time that has elapsed.
|
static String |
getString(byte[] input)
Returns the UTF-8 String for the given byte array, suppressing
UnsupportedEncodingException (in lieu of log message)
|
static String |
getTimeFromLong(long diff)
Returns a formatted String from time.
|
static String |
hash(byte[] bytes,
String algorithm)
Hashes a byte array using the specified algorithm and returns the result as a
String of hexadecimal numbers.
|
static String |
hash(String data)
Hashes a String using the Md5 algorithm and returns the result as a
String of hexadecimal numbers.
|
static String |
hash(String data,
String algorithm)
Hashes a String using the specified algorithm and returns the result as a
String of hexadecimal numbers.
|
static boolean |
isBase32(String data)
Validates a string to ensure all its bytes are in the Base32 alphabet.
|
static boolean |
isValidEmailAddress(String address)
Returns true if the string passed in is a valid Email address.
|
static String |
randomString(int length)
Returns a random String of numbers and letters (lower and upper case)
of the specified length.
|
static String |
removeXSSCharacters(String input)
Removes characters likely to enable Cross Site Scripting attacks from the
provided input string.
|
static String |
replace(String string,
String oldString,
String newString)
Replaces all instances of oldString with newString in string.
|
static String |
replace(String line,
String oldString,
String newString,
int[] count)
Replaces all instances of oldString with newString in line.
|
static String |
replaceIgnoreCase(String line,
String oldString,
String newString)
Replaces all instances of oldString with newString in line with the
added feature that matches of newString in oldString ignore case.
|
static String |
replaceIgnoreCase(String line,
String oldString,
String newString,
int[] count)
Replaces all instances of oldString with newString in line with the
added feature that matches of newString in oldString ignore case.
|
static Collection<String> |
stringToCollection(String string)
Returns a comma-delimitted list of Strings as a Collection.
|
static String |
stripTags(String in)
This method takes a string and strips out all tags except
tags while still leaving the tag body intact. |
static String[] |
toLowerCaseWordArray(String text)
Converts a line of text into an array of lower case words using a
BreakIterator.wordInstance().
|
static String |
unescapeFromXML(String string)
Unescapes the String by converting XML escape sequences back into normal
characters.
|
static String |
validateDomainName(String domain)
Returns a valid domain name, possibly as an ACE-encoded IDN
(per RFC 3490).
|
static String |
wordWrap(String input,
int width,
Locale locale)
Reformats a string where lines that are longer than width
are split apart at the earliest wordbreak or at maxLength, whichever is
sooner.
|
static String |
zeroPadString(String string,
int length)
Pads the supplied String with 0's to the specified length and returns
the result as a new String.
|
public static String replace(String string, String oldString, String newString)
string
- the String to search to perform replacements on.oldString
- the String that should be replaced by newString.newString
- the String that will replace all instances of oldString.public static String replaceIgnoreCase(String line, String oldString, String newString)
line
- the String to search to perform replacements onoldString
- the String that should be replaced by newStringnewString
- the String that will replace all instances of oldStringpublic static String replaceIgnoreCase(String line, String oldString, String newString, int[] count)
line
- the String to search to perform replacements onoldString
- the String that should be replaced by newStringnewString
- the String that will replace all instances of oldStringcount
- a value that will be updated with the number of replaces
performed.public static String replace(String line, String oldString, String newString, int[] count)
line
- the String to search to perform replacements on.oldString
- the String that should be replaced by newString.newString
- the String that will replace all instances of oldString.public static String stripTags(String in)
in
- the text to be converted.public static String escapeHTMLTags(String in)
in
- the text to be converted.public static String escapeHTMLTags(String in, boolean includeLF)
in
- the text to be converted.includeLF
- set to true to replace \n with public static String hash(String data)
A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.
In Jive, every time a user logs in, we simply take their plain text password, compute the hash, and compare the generated hash to the stored hash. Since it is almost impossible that two passwords will generate the same hash, we know if the user gave us the correct password or not. The only negative to this system is that password recovery is basically impossible. Therefore, a reset password method is used instead.
data
- the String to compute the hash of.public static String hash(String data, String algorithm)
A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.
In Jive, every time a user logs in, we simply take their plain text password, compute the hash, and compare the generated hash to the stored hash. Since it is almost impossible that two passwords will generate the same hash, we know if the user gave us the correct password or not. The only negative to this system is that password recovery is basically impossible. Therefore, a reset password method is used instead.
data
- the String to compute the hash of.algorithm
- the name of the algorithm requested.public static String hash(byte[] bytes, String algorithm)
A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.
In Jive, every time a user logs in, we simply take their plain text password, compute the hash, and compare the generated hash to the stored hash. Since it is almost impossible that two passwords will generate the same hash, we know if the user gave us the correct password or not. The only negative to this system is that password recovery is basically impossible. Therefore, a reset password method is used instead.
bytes
- the byte array to compute the hash of.algorithm
- the name of the algorithm requested.public static String encodeHex(byte[] bytes)
Method by Santeri Paavolainen, Helsinki Finland 1996
(c) Santeri Paavolainen, Helsinki Finland 1996
Distributed under LGPL.
bytes
- an array of bytes to convert to a hex-stringpublic static byte[] decodeHex(String hex)
hex
- a hex encoded String to transform into a byte array.public static String encodeBase64(String data)
data
- a String to encode.public static String encodeBase64(byte[] data)
data
- a byte array to encode.public static byte[] decodeBase64(String data)
data
- a base64 encoded String to decode.public static String encodeBase32(String data)
data
- a String to encode.public static String encodeBase32(byte[] data)
data
- a byte array to encode.public static byte[] decodeBase32(String data)
data
- a base32 encoded String to decode.public static boolean isBase32(String data)
data
- the string to testpublic static String[] toLowerCaseWordArray(String text)
This method is under the Jive Open Source Software License and was written by Mark Imbriaco.
text
- a String of text to convert into an array of wordspublic static String randomString(int length)
The specified length must be at least one. If not, the method will return null.
length
- the desired length of the random String to return.public static String chopAtWord(String string, int length)
length
, the String will be chopped
there. If no newline or whitespace is found in string
up to
the index length
, the String will chopped at length
.
For example, chopAtWord("This is a nice String", 10) will return "This is a" which is the first word boundary less than or equal to 10 characters into the original String.
string
- the String to chop.length
- the index in string
to start looking for a
whitespace boundary at.string
whose length is less than or
equal to length
, and that is chopped at whitespace.public static String wordWrap(String input, int width, Locale locale)
Please note that this method can be lossy - trailing spaces on wrapped lines may be trimmed.
input
- the String to reformat.width
- the maximum length of any one line.public static String escapeForSQL(String string)
string
- the string to escape.public static String escapeForXML(String string)
string
- the string to escape.public static String unescapeFromXML(String string)
string
- the string to unescape.public static String zeroPadString(String string, int length)
string
- the original String to pad.length
- the desired length of the new padded String.public static String dateToMillis(Date date)
public static String getElapsedTime(long delta)
delta
- the elapsed time.public static String getTimeFromLong(long diff)
diff
- the amount of elapsed time.public static String collectionToString(Collection<String> collection)
public static Collection<String> stringToCollection(String string)
public static boolean contains(String[] array, String item)
array
- item
- public static String abbreviate(String str, int maxWidth)
user1@jivesoftware.com/homeand a maximum length of 20 characters, the abbreviate method will return:
user1@jivesoftware.c...
str
- the String to abbreviate.maxWidth
- the maximum size of the string, minus the ellipsis.public static boolean isValidEmailAddress(String address)
address
- Email address to test for validity.public static String validateDomainName(String domain)
domain
- Proposed domain nameIllegalArgumentException
- The given domain name is not validpublic static String removeXSSCharacters(String input)
< > " ' % ; ) ( & + -
input
- the string to be scrubbedpublic static byte[] getBytes(String input)
input
- The source stringpublic static String getString(byte[] input)
input
- The source byte arrayCopyright © 2003-2008 Jive Software.