Jive Forums API (5.5.20.2-oracle) Developer Javadocs

com.jivesoftware.util
Class StringUtils

java.lang.Object
  extended by com.jivesoftware.util.StringUtils

public class StringUtils
extends java.lang.Object

Utility class to peform common String manipulation algorithms.


Field Summary
static java.lang.String OTHER_SAFE_CHARACTERS
          An array of non-alphanumeric, non-space characters which are web safe.
 
Method Summary
static java.lang.String abbreviate(java.lang.String str, int maxWidth)
          Abbreviates a string to a specified length using chop(String, int) and then adds an ellipsis if the input is greater than the maxWidth.
static java.lang.String chop(java.lang.String string, int length)
          Unintelligently chops a string at a given length.
static java.lang.String chopAtWord(java.lang.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 java.lang.String chopAtWord(java.lang.String string, int length, int minLength)
          Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before.
static java.lang.String chopAtWordsAround(java.lang.String input, java.lang.String[] wordList, int numChars)
          Returns a substring of the given string which represents the words around the given word.
static java.lang.String dateToMillis(java.util.Date date)
          Formats a Date as a String.
static long decodeAlphaNumeric(java.lang.String encoded)
          Decodes the alphanumeric string.
static byte[] decodeBase64(byte[] bytes)
          Decodes a BASE64 encoded byte array.
static java.lang.String decodeBase64(java.lang.String data)
          Decodes a base64 String.
static byte[] decodeHex(java.lang.String hex)
          Turns a hex encoded string into a byte array.
static java.lang.String encodeAlphaNumeric(long toEncode)
          Encodes the supplied long as an alphanumeric string.
static java.lang.String encodeBase64(byte[] data)
          Encodes a byte array into a base64 String.
static java.lang.String encodeBase64(java.lang.String data)
          Encodes a String as a base64 String.
static java.lang.String encodeHex(byte[] bytes)
          Turns an array of bytes into a String representing each byte as an unsigned hex number.
static java.lang.String escapeForSQL(java.lang.String string)
          Escapes all necessary characters in the String so that it can be used in SQL
static java.lang.String escapeForXML(java.lang.String string)
          Escapes all necessary characters in the String so that it can be used in an XML doc.
static java.lang.String escapeHTMLTags(java.lang.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 java.lang.String getSpacer(java.lang.String spacer, int num)
          Creates a spacing element given a spacer string and a count (num of times to repeat the spacer.
static java.lang.String getValidEmailDomains()
           
static java.lang.String hash(java.lang.String data)
          Hashes a String using the Md5 algorithm and returns the result as a String of hexadecimal numbers.
static java.lang.String highlightWords(java.lang.String string, java.lang.String[] words, java.lang.String startHighlight, java.lang.String endHighlight)
          Highlights words in a string.
static boolean isValidEmailAddress(java.lang.String addr)
          Validate an email address.
static boolean isWebSafeString(java.lang.String str)
          Tests to make certain a string is a web safe string.
static boolean isWebSafeString(java.lang.String str, java.lang.String otherSafeCharacters)
          Tests to make certain a string is a web safe string.
static java.lang.String randomString(int length)
          Returns a random String of numbers and letters (lower and upper case) of the specified length.
static java.lang.String removeIgnorableCharacters(java.lang.String input)
           Removes any character in the given input which are 'ignorable identifiers' in the Java and Unicode language character sets.
static java.lang.String replace(java.lang.String string, java.lang.String oldString, java.lang.String newString)
          Replaces all instances of oldString with newString in string.
static java.lang.String replace(java.lang.String line, java.lang.String oldString, java.lang.String newString, int[] count)
          Replaces all instances of oldString with newString in line.
static java.lang.String replaceIgnoreCase(java.lang.String line, java.lang.String oldString, java.lang.String newString)
          Replaces all instances of oldString with newString in line with the added feature that matches of newString in oldString ignore case.
static java.lang.String replaceIgnoreCase(java.lang.String line, java.lang.String oldString, java.lang.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 java.lang.String stripTags(java.lang.String in)
          This method takes a string and strips out all tags except
tags while still leaving the tag body intact.
static java.lang.String stripTags(java.lang.String in, boolean stripBRTag)
          This method takes a string and strips out all tags while still leaving the tag body intact.
static java.lang.String[] toLowerCaseWordArray(java.lang.String text)
          Converts a line of text into an array of lower case words using a BreakIterator.wordInstance().
static java.lang.String unescapeFromXML(java.lang.String string)
          Unescapes the String by converting XML escape sequences back into normal characters.
static java.lang.String URLEncode(java.lang.String original, java.lang.String charset)
          Encodes URI string.
static java.lang.String urlEncodeAndCleanSpaceChars(java.lang.String str)
          Encodes a str using UTF-8 character set and the Java URLEncoder.encode() method but then replaces all '+' characters in the encoded string with '%20'.
static java.lang.String wordWrap(java.lang.String input, int width, java.util.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 java.lang.String zeroPadString(java.lang.String string, int length)
          Pads the supplied String with 0's to the specified length and returns the result as a new String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OTHER_SAFE_CHARACTERS

public static final java.lang.String OTHER_SAFE_CHARACTERS
An array of non-alphanumeric, non-space characters which are web safe.

See Also:
Constant Field Values
Method Detail

getValidEmailDomains

public static java.lang.String getValidEmailDomains()

replace

public static final java.lang.String replace(java.lang.String string,
                                             java.lang.String oldString,
                                             java.lang.String newString)
Replaces all instances of oldString with newString in string.

Parameters:
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
Returns:
a String will all instances of oldString replaced by newString

replaceIgnoreCase

public static final java.lang.String replaceIgnoreCase(java.lang.String line,
                                                       java.lang.String oldString,
                                                       java.lang.String newString)
Replaces all instances of oldString with newString in line with the added feature that matches of newString in oldString ignore case.

Parameters:
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
Returns:
a String will all instances of oldString replaced by newString

replaceIgnoreCase

public static final java.lang.String replaceIgnoreCase(java.lang.String line,
                                                       java.lang.String oldString,
                                                       java.lang.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. The count paramater is set to the number of replaces performed.

Parameters:
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
count - a value that will be updated with the number of replaces performed.
Returns:
a String will all instances of oldString replaced by newString

replace

public static final java.lang.String replace(java.lang.String line,
                                             java.lang.String oldString,
                                             java.lang.String newString,
                                             int[] count)
Replaces all instances of oldString with newString in line. The count Integer is updated with number of replaces.

Parameters:
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
Returns:
a String will all instances of oldString replaced by newString

stripTags

public static final java.lang.String stripTags(java.lang.String in)
This method takes a string and strips out all tags except
tags while still leaving the tag body intact.

Parameters:
in - the text to be converted.
Returns:
the input string with all tags removed.

stripTags

public static final java.lang.String stripTags(java.lang.String in,
                                               boolean stripBRTag)
This method takes a string and strips out all tags while still leaving the tag body intact.

Parameters:
in - the text to be converted.
Returns:
the input string with all tags removed.

escapeHTMLTags

public static final java.lang.String escapeHTMLTags(java.lang.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.

Parameters:
in - the text to be converted.
Returns:
the input string with the characters '<' and '>' replaced with their HTML escape sequences.

hash

public static final java.lang.String hash(java.lang.String data)
Hashes a String using the Md5 algorithm and returns the result as a String of hexadecimal numbers. This method is synchronized to avoid excessive MessageDigest object creation. If calling this method becomes a bottleneck in your code, you may wish to maintain a pool of MessageDigest objects instead of using this method.

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.

Parameters:
data - the String to compute the hash of.
Returns:
a hashed version of the passed-in String

encodeHex

public static final java.lang.String encodeHex(byte[] bytes)
Turns an array of bytes into a String representing each byte as an unsigned hex number.

Parameters:
bytes - an array of bytes to convert to a hex-string
Returns:
generated hex string

decodeHex

public static final byte[] decodeHex(java.lang.String hex)
Turns a hex encoded string into a byte array. It is specifically meant to "reverse" the toHex(byte[]) method.

Parameters:
hex - a hex encoded String to transform into a byte array.
Returns:
a byte array representing the hex String[

encodeBase64

public static java.lang.String encodeBase64(java.lang.String data)
Encodes a String as a base64 String.

Parameters:
data - a String to encode.
Returns:
a base64 encoded String.

decodeBase64

public static java.lang.String decodeBase64(java.lang.String data)
Decodes a base64 String.

Parameters:
data - a base64 encoded String to decode.
Returns:
the decoded String.

encodeAlphaNumeric

public static java.lang.String encodeAlphaNumeric(long toEncode)
Encodes the supplied long as an alphanumeric string. Encoding is performed using a 62 digit base made up of all the alphabetic characters in upper and lower case as well as all ten digits.

Parameters:
toEncode - the long value to encode.
Returns:
an alphanumeric String.

decodeAlphaNumeric

public static long decodeAlphaNumeric(java.lang.String encoded)
Decodes the alphanumeric string. Encoding is performed using a 62 digit base made up of all the alphabetic characters in upper and lower case as well as all ten digits.

Parameters:
encoded - the alphanumeric String to decode.
Returns:
the resultant long value.

encodeBase64

public static java.lang.String encodeBase64(byte[] data)
Encodes a byte array into a base64 String.

Parameters:
data - a byte array to encode.
Returns:
a base64 encode String.

decodeBase64

public static final byte[] decodeBase64(byte[] bytes)
Decodes a BASE64 encoded byte array.

Parameters:
bytes - the source array.
Returns:
the decoded array of bytes.

URLEncode

public static java.lang.String URLEncode(java.lang.String original,
                                         java.lang.String charset)
                                  throws java.io.UnsupportedEncodingException
Encodes URI string. This is a replacement for the java.net.URLEncode#encode(String, String) class which is broken under JDK 1.3.

Parameters:
original - the original character sequence
charset - the protocol charset
Returns:
URI character sequence
Throws:
java.io.UnsupportedEncodingException - unsupported character encoding

toLowerCaseWordArray

public static final java.lang.String[] toLowerCaseWordArray(java.lang.String text)
Converts a line of text into an array of lower case words using a BreakIterator.wordInstance().

This method is under the Jive Open Source Software License and was written by Mark Imbriaco.

Parameters:
text - a String of text to convert into an array of words
Returns:
text broken up into an array of words.

randomString

public static final java.lang.String randomString(int length)
Returns a random String of numbers and letters (lower and upper case) of the specified length. The method uses the Random class that is built-in to Java which is suitable for low to medium grade security uses. This means that the output is only pseudo random, i.e., each number is mathematically generated so is not truly random.

The specified length must be at least one. If not, the method will return null.

Parameters:
length - the desired length of the random String to return.
Returns:
a random String of numbers and letters of the specified length.

chop

public static final java.lang.String chop(java.lang.String string,
                                          int length)
Unintelligently chops a string at a given length. If characters of a string are chopped, elipses will be appended.

Parameters:
string - the string to chop.
length - the maximum number of characters to show.
Returns:
the string chopped to the specified length with elipses at the end if the string was longer than the given length.

chopAtWord

public static final java.lang.String chopAtWord(java.lang.String string,
                                                int length,
                                                int minLength)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before. However, if there is a newline character before 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, -1) will return "This is a" which is the first word boundary less than or equal to 10 characters into the original String.

Parameters:
string - the String to chop.
length - the index in string to start looking for a whitespace boundary at.
minLength - the minimum length the word should be chopped at. This is helpful for words with no natural boundaries, ie: "thisisareallylonglonglongword". This must be smaller than length and can be -1 if no minLength is wanted
Returns:
a substring of string whose length is less than or equal to length, and that is chopped at whitespace.

chopAtWord

public static final java.lang.String chopAtWord(java.lang.String string,
                                                int length)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before. However, if there is a newline character before 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.

Parameters:
string - the String to chop.
length - the index in string to start looking for a whitespace boundary at.
Returns:
a substring of string whose length is less than or equal to length, and that is chopped at whitespace.

chopAtWordsAround

public static java.lang.String chopAtWordsAround(java.lang.String input,
                                                 java.lang.String[] wordList,
                                                 int numChars)
Returns a substring of the given string which represents the words around the given word. For example, passing in "This is a quick test a test", "{a,test}" and 5 would return a string of "This is a quick" - that's 5 characters (or to the end of the word, whichever is greater) on either side of "a". Also, since {a,test} is passed in a "a" is found first in the string, we base the substring off of the position of "a". The wordList is really just a list of strings to try - the first one found is used.

Note: The wordList passed in should be lowercase.

Parameters:
input - The string to parse.
wordList - The words to look for - the first one found in the string is used.
numChars - The number of characters on either side to include in the chop.
Returns:
a substring of the given string matching the criteria, otherwise "".

wordWrap

public static java.lang.String wordWrap(java.lang.String input,
                                        int width,
                                        java.util.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. If the width specified is less than 5 or greater than the input Strings length the string will be returned as is.

Please note that this method can be lossy - trailing spaces on wrapped lines may be trimmed.

Parameters:
input - the String to reformat.
width - the maximum length of any one line.
Returns:
a new String with reformatted as needed.

highlightWords

public static final java.lang.String highlightWords(java.lang.String string,
                                                    java.lang.String[] words,
                                                    java.lang.String startHighlight,
                                                    java.lang.String endHighlight)
Highlights words in a string. Words matching ignores case. The actual higlighting method is specified with the start and end higlight tags. Those might be beginning and ending HTML bold tags, or anything else.

This method is under the Jive Open Source Software License and was written by Mark Imbriaco.

Parameters:
string - the String to highlight words in.
words - an array of words that should be highlighted in the string.
startHighlight - the tag that should be inserted to start highlighting.
endHighlight - the tag that should be inserted to end highlighting.
Returns:
a new String with the specified words highlighted.

escapeForSQL

public static final java.lang.String escapeForSQL(java.lang.String string)
Escapes all necessary characters in the String so that it can be used in SQL

Parameters:
string - the string to escape.
Returns:
the string with appropriate characters escaped.

escapeForXML

public static final java.lang.String escapeForXML(java.lang.String string)
Escapes all necessary characters in the String so that it can be used in an XML doc.

Parameters:
string - the string to escape.
Returns:
the string with appropriate characters escaped.

unescapeFromXML

public static final java.lang.String unescapeFromXML(java.lang.String string)
Unescapes the String by converting XML escape sequences back into normal characters.

Parameters:
string - the string to unescape.
Returns:
the string with appropriate characters unescaped.

zeroPadString

public static final java.lang.String zeroPadString(java.lang.String string,
                                                   int length)
Pads the supplied String with 0's to the specified length and returns the result as a new String. For example, if the initial String is "9999" and the desired length is 8, the result would be "00009999". This type of padding is useful for creating numerical values that need to be stored and sorted as character data. Note: the current implementation of this method allows for a maximum length of 64.

Parameters:
string - the original String to pad.
length - the desired length of the new padded String.
Returns:
a new String padded with the required number of 0's.

dateToMillis

public static final java.lang.String dateToMillis(java.util.Date date)
Formats a Date as a String. Depending on how Dates are defined in the database (character data or numberic), the format return will either be a fifteen character long String made up of the Date's padded millisecond value, or will simply be the Date's millesecond value.

Returns:
a Date encoded as a String.

isValidEmailAddress

public static boolean isValidEmailAddress(java.lang.String addr)
Validate an email address. This isn't 100% perfect but should handle just about everything that is in common use.

Parameters:
addr - the email address to validate
Returns:
true if the address is valid, false otherwise

removeIgnorableCharacters

public static java.lang.String removeIgnorableCharacters(java.lang.String input)

Removes any character in the given input which are 'ignorable identifiers' in the Java and Unicode language character sets. This is helpful for things like RSS feeds which sends a message body out which might contain high-order characters. This might happen if the messages has content pasted from MS Word or other applications.

This internally calls Character.isIdentifierIgnorable(char) to determine if a character is ignorable. Please read the Javadocs on that method for a list of ignorable characters.

Parameters:
input - the input to test.
Returns:
the input with any ignoreable characters removed.

getSpacer

public static java.lang.String getSpacer(java.lang.String spacer,
                                         int num)
Creates a spacing element given a spacer string and a count (num of times to repeat the spacer.

Parameters:
spacer - a spacer string ie, " " or "&nbsp;".
num - the number of times to repeat the spacer string.
Returns:
a string with the spacer character repeated.

abbreviate

public static java.lang.String abbreviate(java.lang.String str,
                                          int maxWidth)
Abbreviates a string to a specified length using chop(String, int) and then adds an ellipsis if the input is greater than the maxWidth. Example input:
      This is the body of a great blog post.
 
and a maximum length of 20 characters, the abbreviate method will return:
      This is the body of...
 

Parameters:
str - the String to abbreviate.
maxWidth - the maximum size of the string, minus the ellipsis.
Returns:
the abbreviated String, or null if the string was null.

isWebSafeString

public static boolean isWebSafeString(java.lang.String str)
Tests to make certain a string is a web safe string. This function ensures that the passed in String contains only safe alpha numeric characters and a limited set of other characters. This function should be used when testing basic input like names, subjects, as the only allowed whitespace are:

Also, the following characters are permitted '",.?*@`~-+_=\\/$

Parameters:
str - the string being tested.
Returns:
true if the String is safe for the web and false if it is not.
See Also:
OTHER_SAFE_CHARACTERS

isWebSafeString

public static boolean isWebSafeString(java.lang.String str,
                                      java.lang.String otherSafeCharacters)
Tests to make certain a string is a web safe string. This function ensures that the passed in String contains only safe alpha numeric characters and a limited set of other characters. This function should be used when testing basic input like names, subjects, as the only allowed whitespace are:

Other characters can be permitted by passing in a String of other safe characters.

Parameters:
str - the string being tested.
otherSafeCharacters - other safe characters which can be permitted in the input String.
Returns:
true if the String is safe for the web and false if it is not.

urlEncodeAndCleanSpaceChars

public static java.lang.String urlEncodeAndCleanSpaceChars(java.lang.String str)
Encodes a str using UTF-8 character set and the Java URLEncoder.encode() method but then replaces all '+' characters in the encoded string with '%20'.

Parameters:
str - the string being encoded.
Returns:
URL encoded string that uses '%20' for spaces rather than '+'

Jive Forums Project Page

Copyright © 1999-2006 Jive Software.