com.autonomy.utilities
Class StringUtils

java.lang.Object
  |
  +--com.autonomy.utilities.StringUtils

public class StringUtils
extends java.lang.Object

Library of functions used for working with Strings


Constructor Summary
StringUtils()
          Constructor.
 
Method Summary
static boolean atob(java.lang.String sKey, boolean bDefault)
          Coverts a string to a boolean or returns bDefault if this is not possible
static double atod(java.lang.String sNumber, double dDefault)
          Coverts a string to a double or returns dDefault if this is not possible
static float atof(java.lang.String sNumber, float fDefault)
          Coverts a string to a float or returns fDefault if this is not possible
static int atoi(java.lang.String sNumber, int nDefault)
          Coverts a string to an int or returns nDefault if this is not possible
static long atol(java.lang.String sNumber, long nDefault)
          Coverts a string to a long or returns nDefault if this is not possible
static int binarySearch(java.lang.String[] saInput, char cChar, boolean bReturnNotFound)
          Perform a binary search , finds the first element in sorted array saInput beginning with cChar or the first letter after cChar
static int binarySearch(java.util.Vector vInput, char cChar, boolean bReturnNotFound)
          Perform a binary search , finds the first element in sortedvector vInput beginning with cChar or the first letter after cChar
static int binarySearch(java.util.Vector vInput, java.lang.String sToFind, boolean bReturnNotFound)
          Perform a binary search , finds the first element in sorted vector vInput beginning with cChar or the first letter after cChar
static java.lang.String combine(java.lang.String[] saInput, java.lang.String sSplitCharacter)
          Method to convert an array of Strings into a CSV
static boolean containsIllegalChars(java.lang.String sTest)
          Does a strValid and then returns true if the String contains " ' < > or illegal &
static boolean containsNonAlphaNumericChars(java.lang.String sTest)
          Does a strValid and then returns true if the String contains anything other than letters digits or underscores
static java.lang.String decryptString(java.lang.String sToDecrypt)
          decrypts a string
static java.util.Enumeration deduplicate(java.lang.String[] as)
          Remove any duplicated elements from the array
static java.lang.String encryptString(java.lang.String sToEncrypt)
          encrypts a string
static java.lang.String ensureNoSeparatorAtEnd(java.lang.String sToSlash)
          Removes any path.separator at the end of a string if there isn't one there already
static java.lang.String ensureSeparatorAtEnd(java.lang.String sToSlash)
          Puts a path.separator at the end of a string if there isn't one there already
static java.lang.String getHTMLColorFromString(java.lang.String sToParse)
          parses a string and looks for a HTML "#xxxxxx" type string
static java.lang.String getImageNameFromString(java.lang.String sToParse)
          parses a string and extracts an image name from a background="image.gif" string
static java.lang.String getString(java.io.InputStream inputStream)
          returns a string from an input stream
static int indexOfIgnoreCase(java.lang.String sString1, java.lang.String sString2)
          Returns the index within the first string of the first occurrence of the second string regardless of case.
static int indexOfIgnoreCase(java.lang.String sString1, java.lang.String sString2, int nStartIndex)
          Returns the index within the first string of the first occurrence of the second string regardless of case, starting at the specified index.
static boolean isNotEmpty(java.lang.String s)
           
static int isStringInStringArray(java.lang.String[] saArray, java.lang.String sString, boolean bCaseInsenstive)
          Looks for a string in a string array
static boolean isTrue(java.lang.String sKey)
          Function returns true if the given string, when trimmed and lower cased, is either "yes", "1", "on" or "true".
static java.lang.String javaScriptEscape(java.lang.String sToEscape)
           
static java.lang.String makeUniqueElement(java.lang.String[] asElements, java.lang.String sToCheck)
          Checks whether sToCheck is contained in the array asElements.
static java.lang.String nullToEmpty(java.lang.String sInput)
          If the input string is null, this converts it to empty, otherwise leaves it be
static java.lang.String[] or(java.lang.String[] s1, java.lang.String[] s2)
          A case sensitive string array OR - returns a string array containing only one occurance of any string in s1 and s2
static java.lang.String padWithZeros(java.lang.String sNumber, int n)
          Pads a string out to length n with leading zeros
static java.lang.String pluralise(java.lang.String sSingular, java.lang.String sPlural, int nToTest)
          returns either the singular or the plural based on the value of the number For example:
out.println( "This code is " + StringUtils.pluralise( "mine", "ours", nNumberOfCodeOwners ) + "!" );
static void print(java.lang.String s)
           
static java.lang.String[] quickSort(java.lang.String[] asToSort)
          Quick sort string arrays into lexicgraphical order
static void quickSort(java.util.Vector vec)
          Quick sort vectors into lexicgraphical order
static java.lang.String[] quickSortWithMirror(java.lang.String[] asToSort, java.lang.String[] asToMirror)
          Quick sort string arrays into lexicgraphical order, mirroring the changes in asToMirror
static java.lang.String removeFormatting(java.lang.String sString)
          Removes all formatting characters from a string
static java.lang.String[] split(java.lang.String sInput, java.lang.String sSplitCharacter)
          Method to Split up a CSV String into an array.
static int[] stringArrayToIntArray(java.lang.String[] sToIntArray)
          Quick method to convert a string array to int array
static long[] stringArrayToLongArray(java.lang.String[] sToLongArray)
          Quick method to convert a string array to long array
static java.util.Vector stringArrayToVector(java.lang.Object[] saInput)
          creates a vector containing the same elements as an array
static java.lang.String stringReplace(java.lang.String sInputString, java.lang.String sToReplace, java.lang.String sReplacementString)
          Replace muliple occurrences of the specified string within an input string
static java.lang.String stringReplaceIgnoreCase(java.lang.String sInputString, java.lang.String sToReplace, java.lang.String sReplacementString)
          Replace multiple occurrences of the specified string within an input string, regardless of case
static java.lang.String stripDangerousChars(java.lang.String sString)
          Safety net for catching and stripping < > and ", which can be dangerous when working with dynamically generated HTML pages ( ie JSP pages or Servlets ).
static java.lang.String stripHTML(java.lang.String sIn)
          Strips out all HTML in the given string
static java.lang.String stripNonAlphaNum(java.lang.String sString)
          Removes all non-alphanumeric characters from the string
static boolean strValid(java.lang.String sTest)
          Returns true if the string isn't null or empty
static boolean strValid(java.lang.String sTest, int nMaxLength)
          Returns true if the string isn't null and between 1 and MaxLength chars long Pass in 0 or a negative number for maxlength and it will not check for length
static java.lang.String toSentenceCase(java.lang.String sInput)
          Puts Strings Into This Form
static java.lang.String urlDecode(java.lang.String sUrlEncoded)
          Method to decode URLs, as this is not present in JRE1.1 environments
static java.lang.String[] vectorToStringArray(java.util.Vector vInput)
          creates a vector containing the same elements as an array
static boolean wildcardStringEquals(java.lang.String sWild, java.lang.String sComp, boolean bCaseSensitive)
          compares two strings, sWild may contain wildcards '?' & '*'.
static java.lang.String XMLEscape(java.lang.String sToEscape)
          Performs a basic <, >, ", &, ' XML Escape on the given string
static java.lang.String XMLUnescape(java.lang.String sToEscape)
          XML unescapes the given string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

public StringUtils()
Constructor. As this is a frequently used class, it can be good to have an instance of it

Method Detail

split

public static java.lang.String[] split(java.lang.String sInput,
                                       java.lang.String sSplitCharacter)
Method to Split up a CSV String into an array.

Parameters:
sInput - Input CSV String separated by sSplitCharacter's
sSplitCharacter - Separator characters

combine

public static java.lang.String combine(java.lang.String[] saInput,
                                       java.lang.String sSplitCharacter)
Method to convert an array of Strings into a CSV

Parameters:
saInput - Input Array of Strings
sSplitCharacter - Separator characters
Returns:
String a CSV String separated by the sSplitCharacter

isTrue

public static boolean isTrue(java.lang.String sKey)
Function returns true if the given string, when trimmed and lower cased, is either "yes", "1", "on" or "true". Returns false if sKey is null


ensureSeparatorAtEnd

public static java.lang.String ensureSeparatorAtEnd(java.lang.String sToSlash)
Puts a path.separator at the end of a string if there isn't one there already

Parameters:
sToSlash - to be checked snd possibly have slash appended

ensureNoSeparatorAtEnd

public static java.lang.String ensureNoSeparatorAtEnd(java.lang.String sToSlash)
Removes any path.separator at the end of a string if there isn't one there already

Parameters:
sToSlash - to be checked snd possibly have slash appended

getHTMLColorFromString

public static java.lang.String getHTMLColorFromString(java.lang.String sToParse)
parses a string and looks for a HTML "#xxxxxx" type string

Parameters:
sToParse - string to parse
Returns:
String in form #xxxxxx or null if it cannot be found

getImageNameFromString

public static java.lang.String getImageNameFromString(java.lang.String sToParse)
                                               throws java.lang.StringIndexOutOfBoundsException
parses a string and extracts an image name from a background="image.gif" string

Parameters:
sToParse - string to parse
Returns:
image name or null if it cannot be found
java.lang.StringIndexOutOfBoundsException

nullToEmpty

public static java.lang.String nullToEmpty(java.lang.String sInput)
If the input string is null, this converts it to empty, otherwise leaves it be


strValid

public static boolean strValid(java.lang.String sTest,
                               int nMaxLength)
Returns true if the string isn't null and between 1 and MaxLength chars long Pass in 0 or a negative number for maxlength and it will not check for length

Parameters:
sTest - The string to test

strValid

public static boolean strValid(java.lang.String sTest)
Returns true if the string isn't null or empty

Parameters:
sTest - The string to test

containsIllegalChars

public static boolean containsIllegalChars(java.lang.String sTest)
Does a strValid and then returns true if the String contains " ' < > or illegal &


containsNonAlphaNumericChars

public static boolean containsNonAlphaNumericChars(java.lang.String sTest)
Does a strValid and then returns true if the String contains anything other than letters digits or underscores


quickSort

public static java.lang.String[] quickSort(java.lang.String[] asToSort)
Quick sort string arrays into lexicgraphical order

Parameters:
asToSort - string to sort
Returns:
sorted string

quickSortWithMirror

public static java.lang.String[] quickSortWithMirror(java.lang.String[] asToSort,
                                                     java.lang.String[] asToMirror)
Quick sort string arrays into lexicgraphical order, mirroring the changes in asToMirror

Parameters:
asToSort - string to sort
Returns:
sorted string

quickSort

public static void quickSort(java.util.Vector vec)
Quick sort vectors into lexicgraphical order

Parameters:
vec - vector to sort

or

public static java.lang.String[] or(java.lang.String[] s1,
                                    java.lang.String[] s2)
A case sensitive string array OR - returns a string array containing only one occurance of any string in s1 and s2

Parameters:
s1 - String 1 for ORring
s2 - String 2 for ORring

makeUniqueElement

public static java.lang.String makeUniqueElement(java.lang.String[] asElements,
                                                 java.lang.String sToCheck)
Checks whether sToCheck is contained in the array asElements. If so, adds number to the string until it is no longer an element of the array. Case sensitive.

Parameters:
asElements - Array of strings against which sToCheck should be compared
sToCheck - The string to be checked.
Returns:
The string sToCheck if it is not an element of asElements or sToCheck+ otherwise.

print

public static void print(java.lang.String s)

stringReplace

public static java.lang.String stringReplace(java.lang.String sInputString,
                                             java.lang.String sToReplace,
                                             java.lang.String sReplacementString)
Replace muliple occurrences of the specified string within an input string

Parameters:
sInputString - String to change
sToReplace - The substring to find
sReplacementString - The string to replace sToReplace with.
Returns:
the modified string

stringReplaceIgnoreCase

public static java.lang.String stringReplaceIgnoreCase(java.lang.String sInputString,
                                                       java.lang.String sToReplace,
                                                       java.lang.String sReplacementString)
Replace multiple occurrences of the specified string within an input string, regardless of case

Parameters:
sInputString - String to change
sToReplace - The substring to find
sReplacementString - The string to replace sToReplace with. The case formatting of this string is conserved.
Returns:
the modified string

indexOfIgnoreCase

public static int indexOfIgnoreCase(java.lang.String sString1,
                                    java.lang.String sString2)
Returns the index within the first string of the first occurrence of the second string regardless of case.

Parameters:
sString1 - The string to be search
sString2 - The substring to be found
Returns:
If the second string argument occurs as a substring within this first string argument, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

indexOfIgnoreCase

public static int indexOfIgnoreCase(java.lang.String sString1,
                                    java.lang.String sString2,
                                    int nStartIndex)
Returns the index within the first string of the first occurrence of the second string regardless of case, starting at the specified index.

Parameters:
sString1 - The string to be search
sString2 - The substring to be found
Returns:
If the second string argument occurs as a substring within this first string argument at a starting index no smaller than fromIndex, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

XMLEscape

public static java.lang.String XMLEscape(java.lang.String sToEscape)
Performs a basic <, >, ", &, ' XML Escape on the given string

Returns:
escaped string

XMLUnescape

public static java.lang.String XMLUnescape(java.lang.String sToEscape)
XML unescapes the given string. &, >, <, " and ' and all &#N; entries with 124 < N < 256 are converted to their respective characters.

Returns:
unescaped string

binarySearch

public static int binarySearch(java.lang.String[] saInput,
                               char cChar,
                               boolean bReturnNotFound)
Perform a binary search , finds the first element in sorted array saInput beginning with cChar or the first letter after cChar

Parameters:
saInput - The array in which to search [this must be sorted]
cChar - The string to find
bReturnNotFound - will return -1 if not found, otherwise will return the earliest element starting with the closest char after cChar
Returns:
the first occurrence of the character, or -1 if it wasn't found

binarySearch

public static int binarySearch(java.util.Vector vInput,
                               char cChar,
                               boolean bReturnNotFound)
Perform a binary search , finds the first element in sortedvector vInput beginning with cChar or the first letter after cChar

Parameters:
vInput - The vector in which to search [this must be sorted]
cChar - The string to find
bReturnNotFound - will return -1 if not found, otherwise will return the earliest element starting with the closest char after cChar
Returns:
the first occurrence of the character, or -1 if it wasn't found

binarySearch

public static int binarySearch(java.util.Vector vInput,
                               java.lang.String sToFind,
                               boolean bReturnNotFound)
Perform a binary search , finds the first element in sorted vector vInput beginning with cChar or the first letter after cChar

Parameters:
vInput - The vector in which to search [this must be sorted]
sToFind - The string to find
bReturnNotFound - if true will return -1 if not found, otherwise will return the earliest element starting with the closest String after sToFind
Returns:
the first occurrence of the character, or -1 if it wasn't found

stringArrayToIntArray

public static int[] stringArrayToIntArray(java.lang.String[] sToIntArray)
Quick method to convert a string array to int array

Returns:
int array of converted numbers

stringArrayToLongArray

public static long[] stringArrayToLongArray(java.lang.String[] sToLongArray)
Quick method to convert a string array to long array

Returns:
int array of converted numbers

isStringInStringArray

public static int isStringInStringArray(java.lang.String[] saArray,
                                        java.lang.String sString,
                                        boolean bCaseInsenstive)
Looks for a string in a string array

Parameters:
saArray - String array to parse
sString - string to find
bCaseInsenstive - case sensitivity flag
Returns:
Index of string in the array or -1 if not found

stringArrayToVector

public static java.util.Vector stringArrayToVector(java.lang.Object[] saInput)
creates a vector containing the same elements as an array

Returns:
vector

vectorToStringArray

public static java.lang.String[] vectorToStringArray(java.util.Vector vInput)
creates a vector containing the same elements as an array

Returns:
vector

atoi

public static int atoi(java.lang.String sNumber,
                       int nDefault)
Coverts a string to an int or returns nDefault if this is not possible


atol

public static long atol(java.lang.String sNumber,
                        long nDefault)
Coverts a string to a long or returns nDefault if this is not possible


atof

public static float atof(java.lang.String sNumber,
                         float fDefault)
Coverts a string to a float or returns fDefault if this is not possible


atod

public static double atod(java.lang.String sNumber,
                          double dDefault)
Coverts a string to a double or returns dDefault if this is not possible


atob

public static boolean atob(java.lang.String sKey,
                           boolean bDefault)
Coverts a string to a boolean or returns bDefault if this is not possible


stripDangerousChars

public static java.lang.String stripDangerousChars(java.lang.String sString)
Safety net for catching and stripping < > and ", which can be dangerous when working with dynamically generated HTML pages ( ie JSP pages or Servlets ).

Returns:
modified string

stripNonAlphaNum

public static java.lang.String stripNonAlphaNum(java.lang.String sString)
Removes all non-alphanumeric characters from the string

Returns:
modified string

toSentenceCase

public static java.lang.String toSentenceCase(java.lang.String sInput)
Puts Strings Into This Form

Returns:
sentenced cased string

pluralise

public static java.lang.String pluralise(java.lang.String sSingular,
                                         java.lang.String sPlural,
                                         int nToTest)
returns either the singular or the plural based on the value of the number For example:

Parameters:
sSingular - singular word
sPlural - plural of that word
nToTest - a number
Returns:
sSingular if nToTest = 1, sPlural otherwise

padWithZeros

public static java.lang.String padWithZeros(java.lang.String sNumber,
                                            int n)
Pads a string out to length n with leading zeros


urlDecode

public static java.lang.String urlDecode(java.lang.String sUrlEncoded)
Method to decode URLs, as this is not present in JRE1.1 environments


wildcardStringEquals

public static boolean wildcardStringEquals(java.lang.String sWild,
                                           java.lang.String sComp,
                                           boolean bCaseSensitive)
compares two strings, sWild may contain wildcards '?' & '*'. '?' - represents any one character '*' - represents zero or more characters

Parameters:
sWild - first String to compare. May contain wildcards
sComp - second String to compare.
bCaseSensitive - case sensitive or not
Returns:
b true if equal, false if unequal

encryptString

public static java.lang.String encryptString(java.lang.String sToEncrypt)
encrypts a string

Parameters:
sToEncrypt - the string to encrypt
Returns:
encrypted string

decryptString

public static java.lang.String decryptString(java.lang.String sToDecrypt)
decrypts a string

Parameters:
sToDecrypt - the string to decrypt
Returns:
decrypted string

getString

public static java.lang.String getString(java.io.InputStream inputStream)
returns a string from an input stream

Parameters:
inputStream - InputStream
Returns:
String, empty string is returned if errors occur or no input stream

removeFormatting

public static java.lang.String removeFormatting(java.lang.String sString)
Removes all formatting characters from a string

Parameters:
sString - the string to clean
Returns:
a copy of the parameter string with formatting characters removed.

deduplicate

public static java.util.Enumeration deduplicate(java.lang.String[] as)
Remove any duplicated elements from the array

Returns:
Enumeration of non-duplicate elements

stripHTML

public static java.lang.String stripHTML(java.lang.String sIn)
Strips out all HTML in the given string

Parameters:
sIn - string to strip HTML from
Returns:
String with HTML removed

javaScriptEscape

public static java.lang.String javaScriptEscape(java.lang.String sToEscape)

isNotEmpty

public static boolean isNotEmpty(java.lang.String s)