utils
Class StringUtils

java.lang.Object
  extended by utils.StringUtils

public class StringUtils
extends java.lang.Object

Copyright DocJava, inc. User: lyon Date: Sep 6, 2004 Time: 8:35:04 AM


Field Summary
static int CHAR_BUFFER_SIZE
          The size of the buffer to use when working with I/O (4 kB).
static java.lang.String MONTH_PATTERN1
           
static java.lang.String MONTH_PATTERN2
           
static java.lang.String MONTH_PATTERN3
           
static char NEW_LINE
           
static java.lang.String NEW_LINE_STRING
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static void add(java.util.Vector v, java.lang.String[] strings)
          call this if you want to merge a list of strings into a vector list.
static int addTokens(java.lang.String l)
          Determine how many tokens are in a string
static boolean contains(java.lang.String s1, java.lang.String s2)
           
static java.lang.String convertNativeToUnicode(java.lang.String input, java.lang.String charset)
          Convert a string from a native encoding to unicode.
static java.lang.String convertUnicodeToNative(java.lang.String source, java.lang.String charset)
          Convert a string from unicode to bytes in a native encoding.
static int csv2int(java.lang.String x)
           
static boolean endsWithDigit(java.lang.String s)
           
static java.lang.String[] getAsArray(java.lang.String s, java.lang.String token)
           
static char getFirstChar(java.lang.String s)
           
static java.lang.String getFormat(double n)
           
static java.lang.String getIntString(int i, int numberOfDigits)
          Will format a number to be exactly numberOfDigits long
static double getMax(java.lang.String text)
           
static double getMin(java.lang.String text)
           
static double getMoney(java.lang.String s)
          Assume you are in the demoniation of $ and trim out the spaces to obtain
static java.lang.String getMoneyString(double v)
           
static int getNumberOfDigits(java.lang.String s)
           
static int getNumberOfShares(java.lang.String source)
           
static java.lang.Object[] getObjectsThatContain(java.lang.Object[] list, java.lang.String contain)
           
static java.lang.String getOneBigString(java.lang.String[] s)
          Concatenate an array of strings into one large string, with carrage returns.
static java.lang.String getRestOfString(java.lang.String startString, java.lang.String searchString)
           
static java.lang.String[] getStrings(java.lang.Object[] o)
           
static java.lang.String[] getStringsThatContain(java.lang.String[] list, java.lang.String contain)
          Scan a list for the string contain.
static java.lang.String[] getStringsThatContain(java.lang.String[] list, java.lang.String[] contain)
           
static java.lang.String[] getStringsThatDontContain(java.lang.String[] list, java.lang.String contain)
           
static java.lang.String[] getStringsThatEndWith(java.lang.String[] list, java.lang.String end)
           
static java.lang.String getSuffix(java.lang.String s, java.lang.String suffixDelimiter)
          Search a string for a substring.
static java.util.Date getTerminationDate(java.lang.String s)
           
static java.lang.String getUtf7Encoding(java.lang.String input)
          Here is my little home-brew way to convert to 0..127 based ascii chars.
static int getYahooInt(java.lang.String s)
           
static int heuristicStringMatch(java.lang.String s1, java.lang.String s2)
           
static void hexDump(java.lang.String s)
           
static boolean isGoodChar(char c)
           
static java.lang.String isolate(java.lang.String source, java.lang.String prefix, java.lang.String postfix)
          Look in the source for the phrase that begins with the prefix, ends with the postfix and return the string that is contained by them
static boolean isYes(java.lang.String s1)
           
static java.lang.String join(double[] array, java.lang.String seperator)
          Creates a new String object, containing the elements of a supplied array, joined by a given seperator.
static java.lang.String join(float[] array, java.lang.String seperator)
          Creates a new String object, containing the elements of a supplied array, joined by a given seperator.
static java.lang.String join(int[] array, java.lang.String seperator)
          Creates a new String object, containing the elements of a supplied array, joined by a given seperator.
static java.lang.String join(long[] array, java.lang.String seperator)
          Creates a new String object, containing the elements of a supplied array, joined by a given seperator.
static java.lang.String join(java.lang.Object[] array, java.lang.String seperator)
          Creates a new String object, containing the elements of a supplied array, joined by a given seperator.
static int locate(java.lang.String[] symbols, java.lang.String searchString)
           
static java.lang.StringBuffer lowerCaseFirstLetter(java.lang.String s)
           
static void main(java.lang.String[] args)
           
static java.lang.String[] merge(java.lang.String[] s1, java.lang.String[] s2)
          Merge string array s1 and array s2
static java.lang.String removeChar(java.lang.String s, char oldChar)
           
static java.lang.String removeNewLines(java.lang.String s)
           
static java.lang.String removeOddCharacters(java.lang.String s)
           
static java.lang.String repeat(java.lang.String source, int count)
          Creates a new string that contains the provided string a number of times.
static java.lang.String replace(java.lang.String in, java.lang.String t1, java.lang.String t2)
          Replaces all occurrences of t1 by t2 in in.
static java.lang.String replaceAll(java.lang.String s, char searchChar, char replaceChar)
           
static java.lang.String replaceAll(java.lang.String inputString, java.lang.String[] searchString, java.lang.String replaceString)
          Replace all occurances of the searchString in the inputString with the replaceString
static java.lang.String replaceAll(java.lang.String s, java.lang.String searchString, java.lang.String replaceString)
          replaceAll
static java.lang.String replaceAllIgnoreCase(java.lang.String inputString, java.lang.String[] searchString, java.lang.String replaceString)
           
static java.lang.String replaceAllSb(java.lang.String s, java.lang.String orig, java.lang.String dest)
          Method replaceAll.
static java.lang.String replaceChar(java.lang.String s, char oldChar, char newChar)
          Replace all instances of the oldChar with the newChar and create a new string;
static java.lang.String replaceControlCharsWith(java.lang.String s, char replaceChar)
          This routine preserves letters, digits commas spaces and '/'.
static java.lang.String replaceFirstInstance(java.lang.String inputString, java.lang.String searchString, java.lang.String replacementString)
           
static java.lang.String reverse(java.lang.String str)
          Reverse a String, null string returns null.
static void reverseArray(java.lang.Object[] array)
          Reverses an array.
static java.lang.String[] reverseSort(java.lang.String[] members)
          Sort by the tail-end first.
static java.lang.String[] sort(java.lang.String[] members)
          Sort by the front-end first.
static java.lang.String stackTrace(java.lang.Throwable throwable)
          Get the stack trace from a Throwable as a String.
static java.lang.String stripBack(java.lang.String s, char c)
          General-purpose utility function for removing characters from back of string
static java.lang.String stripBack(java.lang.String s, java.lang.String remove)
          General-purpose utility function for removing characters from back of string
static java.lang.String stripFollowing(java.lang.String s, java.lang.String suffix)
          If s contains suffix, chop the rest off.
static java.lang.String stripFront(java.lang.String s, char c)
          General-purpose utility function for removing characters from front of string
static java.lang.String stripFront(java.lang.String s, java.lang.String remove)
          General-purpose utility function for removing characters from front of string
static java.lang.String stripFrontBack(java.lang.String src, java.lang.String head, java.lang.String tail)
          General-purpose utility function for removing characters from the front and back of string
static java.lang.String stripPrefix(java.lang.String s, java.lang.String prefix)
           
static java.lang.String stripQuotes(java.lang.String s)
           
static java.lang.String stripSuffix(java.lang.String s, java.lang.String suffix)
          If s ends with suffix, chop it off.
static java.lang.String sub(java.lang.String line, java.lang.String stringToEliminate, java.lang.String replacementString)
           
static void testBug()
           
static void testGetIntString()
           
static void testGetMax()
           
static void testReplace()
           
static java.lang.String trunc(java.lang.String s, int size)
           
static java.lang.String[] upperCase(java.lang.String[] s)
           
static java.lang.String upperCaseFirstLetter(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEW_LINE

public static char NEW_LINE

NEW_LINE_STRING

public static java.lang.String NEW_LINE_STRING

MONTH_PATTERN3

public static final java.lang.String MONTH_PATTERN3
See Also:
Constant Field Values

MONTH_PATTERN2

public static final java.lang.String MONTH_PATTERN2
See Also:
Constant Field Values

MONTH_PATTERN1

public static final java.lang.String MONTH_PATTERN1
See Also:
Constant Field Values

CHAR_BUFFER_SIZE

public static int CHAR_BUFFER_SIZE
The size of the buffer to use when working with I/O (4 kB).

Constructor Detail

StringUtils

public StringUtils()
Method Detail

stripBack

public static java.lang.String stripBack(java.lang.String s,
                                         char c)
General-purpose utility function for removing characters from back of string

Parameters:
s - The string to process
c - The character to remove
Returns:
The resulting string

stripBack

public static java.lang.String stripBack(java.lang.String s,
                                         java.lang.String remove)
General-purpose utility function for removing characters from back of string

Parameters:
s - The string to process
remove - A string containing the set of characters to remove
Returns:
The resulting string

stripFront

public static java.lang.String stripFront(java.lang.String s,
                                          char c)
General-purpose utility function for removing characters from front of string

Parameters:
s - The string to process
c - The character to remove
Returns:
The resulting string

stripFront

public static java.lang.String stripFront(java.lang.String s,
                                          java.lang.String remove)
General-purpose utility function for removing characters from front of string

Parameters:
s - The string to process
remove - A string containing the set of characters to remove
Returns:
The resulting string

stripFrontBack

public static java.lang.String stripFrontBack(java.lang.String src,
                                              java.lang.String head,
                                              java.lang.String tail)
General-purpose utility function for removing characters from the front and back of string

Parameters:
src - The string to process
head - exact string to strip from head
tail - exact string to strip from tail
Returns:
The resulting string

lowerCaseFirstLetter

public static java.lang.StringBuffer lowerCaseFirstLetter(java.lang.String s)

upperCaseFirstLetter

public static java.lang.String upperCaseFirstLetter(java.lang.String s)
Parameters:
s - to be left unchanged, new string is constructed
Returns:
the input string with the first letter uppercased

replaceChar

public static java.lang.String replaceChar(java.lang.String s,
                                           char oldChar,
                                           char newChar)
Replace all instances of the oldChar with the newChar and create a new string;

Parameters:
s - the old string is unchanged
oldChar - the old char to search for
newChar - the new char to replace it with
Returns:
a new string that is the same size as the old string

removeChar

public static java.lang.String removeChar(java.lang.String s,
                                          char oldChar)

sub

public static java.lang.String sub(java.lang.String line,
                                   java.lang.String stringToEliminate,
                                   java.lang.String replacementString)

replaceFirstInstance

public static java.lang.String replaceFirstInstance(java.lang.String inputString,
                                                    java.lang.String searchString,
                                                    java.lang.String replacementString)

replaceAll

public static java.lang.String replaceAll(java.lang.String s,
                                          java.lang.String searchString,
                                          java.lang.String replaceString)
replaceAll

Parameters:
s - input String
searchString - The string to searchFor
replaceString - The string to replace it with
Returns:
new string

hexDump

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

replaceAll

public static java.lang.String replaceAll(java.lang.String inputString,
                                          java.lang.String[] searchString,
                                          java.lang.String replaceString)
Replace all occurances of the searchString in the inputString with the replaceString

Parameters:
inputString - left unmolested
searchString - an array of strings to search for
replaceString - the new string to replace them with
Returns:
a new string a brand new string

replaceAllIgnoreCase

public static java.lang.String replaceAllIgnoreCase(java.lang.String inputString,
                                                    java.lang.String[] searchString,
                                                    java.lang.String replaceString)

testBug

public static void testBug()

testReplace

public static void testReplace()

addTokens

public static int addTokens(java.lang.String l)
Determine how many tokens are in a string

Parameters:
l - The string to be processed
Returns:
the number of tokens

replaceAll

public static java.lang.String replaceAll(java.lang.String s,
                                          char searchChar,
                                          char replaceChar)

replaceAllSb

public static java.lang.String replaceAllSb(java.lang.String s,
                                            java.lang.String orig,
                                            java.lang.String dest)
Method replaceAll.

Returns:
String

getFirstChar

public static char getFirstChar(java.lang.String s)

getNumberOfDigits

public static int getNumberOfDigits(java.lang.String s)

contains

public static boolean contains(java.lang.String s1,
                               java.lang.String s2)
Parameters:
s1 -
s2 -
Returns:
true if s1.contains(s2)

stripSuffix

public static java.lang.String stripSuffix(java.lang.String s,
                                           java.lang.String suffix)
If s ends with suffix, chop it off.

Parameters:
s -
suffix -
Returns:

stripFollowing

public static java.lang.String stripFollowing(java.lang.String s,
                                              java.lang.String suffix)
If s contains suffix, chop the rest off.

Parameters:
s -
suffix -
Returns:

stripPrefix

public static java.lang.String stripPrefix(java.lang.String s,
                                           java.lang.String prefix)

getStringsThatEndWith

public static java.lang.String[] getStringsThatEndWith(java.lang.String[] list,
                                                       java.lang.String end)

getStringsThatContain

public static java.lang.String[] getStringsThatContain(java.lang.String[] list,
                                                       java.lang.String[] contain)

getStringsThatContain

public static java.lang.String[] getStringsThatContain(java.lang.String[] list,
                                                       java.lang.String contain)
Scan a list for the string contain. If the string is in the list add the string to the list.

Parameters:
list - to search
contain - thing to search for
Returns:
a possibly null return;

getStringsThatDontContain

public static java.lang.String[] getStringsThatDontContain(java.lang.String[] list,
                                                           java.lang.String contain)

getObjectsThatContain

public static java.lang.Object[] getObjectsThatContain(java.lang.Object[] list,
                                                       java.lang.String contain)

stripQuotes

public static java.lang.String stripQuotes(java.lang.String s)

getAsArray

public static java.lang.String[] getAsArray(java.lang.String s,
                                            java.lang.String token)

getUtf7Encoding

public static java.lang.String getUtf7Encoding(java.lang.String input)
Here is my little home-brew way to convert to 0..127 based ascii chars.

Parameters:
input -
Returns:
input string anded with 0x7f.

testGetMax

public static void testGetMax()

getMin

public static double getMin(java.lang.String text)
Parameters:
text -
Returns:

getMax

public static double getMax(java.lang.String text)

isolate

public static java.lang.String isolate(java.lang.String source,
                                       java.lang.String prefix,
                                       java.lang.String postfix)
Look in the source for the phrase that begins with the prefix, ends with the postfix and return the string that is contained by them

Parameters:
source - the string to search.
prefix - the phrase preceeding the one you are looking for
postfix - the phase following the one you are looking for
Returns:
infix null if prefix and or postfix are not present

removeNewLines

public static java.lang.String removeNewLines(java.lang.String s)

removeOddCharacters

public static java.lang.String removeOddCharacters(java.lang.String s)

getTerminationDate

public static java.util.Date getTerminationDate(java.lang.String s)

getYahooInt

public static int getYahooInt(java.lang.String s)

merge

public static java.lang.String[] merge(java.lang.String[] s1,
                                       java.lang.String[] s2)
Merge string array s1 and array s2

Parameters:
s1 - non-null string array
s2 - non-null string array
Returns:
new array whose length=s1.length+s2.length

getOneBigString

public static java.lang.String getOneBigString(java.lang.String[] s)
Concatenate an array of strings into one large string, with carrage returns.

Parameters:
s -
Returns:

locate

public static int locate(java.lang.String[] symbols,
                         java.lang.String searchString)

upperCase

public static java.lang.String[] upperCase(java.lang.String[] s)

endsWithDigit

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

heuristicStringMatch

public static int heuristicStringMatch(java.lang.String s1,
                                       java.lang.String s2)

getMoney

public static double getMoney(java.lang.String s)
Assume you are in the demoniation of $ and trim out the spaces to obtain

Parameters:
s - input string
Returns:
double precision version of the money.

isYes

public static boolean isYes(java.lang.String s1)

getMoneyString

public static java.lang.String getMoneyString(double v)

getIntString

public static java.lang.String getIntString(int i,
                                            int numberOfDigits)
Will format a number to be exactly numberOfDigits long

Parameters:
i - the number to format
numberOfDigits - if i is longer than number of digits, this does not work.
Returns:
a string of type 000...999 for parameters i,3

testGetIntString

public static void testGetIntString()

getNumberOfShares

public static int getNumberOfShares(java.lang.String source)

csv2int

public static int csv2int(java.lang.String x)

getFormat

public static java.lang.String getFormat(double n)

getStrings

public static java.lang.String[] getStrings(java.lang.Object[] o)

add

public static void add(java.util.Vector v,
                       java.lang.String[] strings)
call this if you want to merge a list of strings into a vector list.

Parameters:
v - a vector of strings
strings - an array of strings to add to the vector

replace

public static java.lang.String replace(java.lang.String in,
                                       java.lang.String t1,
                                       java.lang.String t2)
Replaces all occurrences of t1 by t2 in in. jdk1.5 has a method for this, but not jdk1.4.

Parameters:
in -
t1 -
t2 -
Returns:

trunc

public static java.lang.String trunc(java.lang.String s,
                                     int size)

getSuffix

public static java.lang.String getSuffix(java.lang.String s,
                                         java.lang.String suffixDelimiter)
Search a string for a substring.

Parameters:
s - string to search
suffixDelimiter - string to search for
Returns:
all the characters that appear after the suffix delimiter


For example:

System.out.println(getSuffix("Zip:06461",":"));


getRestOfString

public static java.lang.String getRestOfString(java.lang.String startString,
                                               java.lang.String searchString)

isGoodChar

public static boolean isGoodChar(char c)

replaceControlCharsWith

public static java.lang.String replaceControlCharsWith(java.lang.String s,
                                                       char replaceChar)
This routine preserves letters, digits commas spaces and '/'. All other chars are replaced with spaces. There may be other chars that you seek to preserve, like !@#$%^&*()-_+=';, but these will be wiped too.... Note: "isGoodChar" is used as the decision function.

Parameters:
s - input this string and replace all the chars with...
replaceChar - the replacement char (i.e., ' ');
Returns:
new string with the control characters replaced

repeat

public static java.lang.String repeat(java.lang.String source,
                                      int count)
Creates a new string that contains the provided string a number of times.

Parameters:
source - The string that will be repeated.
count - The number of times that the string will be repeated.
Returns:
A new String object containing the repeated concatenation result.
Since:
1.0

join

public static java.lang.String join(java.lang.Object[] array,
                                    java.lang.String seperator)
Creates a new String object, containing the elements of a supplied array, joined by a given seperator.

Parameters:
array - The object array containing the elements to join.
seperator - The seperator used to join the string elements.
Returns:
A new String with the join result.
Since:
1.0

join

public static java.lang.String join(double[] array,
                                    java.lang.String seperator)
Creates a new String object, containing the elements of a supplied array, joined by a given seperator.

Parameters:
array - The double array containing the values to join.
seperator - The seperator used to join the string elements.
Returns:
A new String with the join result.
Since:
1.0

join

public static java.lang.String join(float[] array,
                                    java.lang.String seperator)
Creates a new String object, containing the elements of a supplied array, joined by a given seperator.

Parameters:
array - The float array containing the values to join.
seperator - The seperator used to join the string elements.
Returns:
A new String with the join result.
Since:
1.0

join

public static java.lang.String join(int[] array,
                                    java.lang.String seperator)
Creates a new String object, containing the elements of a supplied array, joined by a given seperator.

Parameters:
array - The integer array containing the values to join.
seperator - The seperator used to join the string elements.
Returns:
A new String with the join result.
Since:
1.0

join

public static java.lang.String join(long[] array,
                                    java.lang.String seperator)
Creates a new String object, containing the elements of a supplied array, joined by a given seperator.

Parameters:
array - The long array containing the values to join.
seperator - The seperator used to join the string elements.
Returns:
A new String with the join result.
Since:
1.0

reverse

public static java.lang.String reverse(java.lang.String str)
Reverse a String, null string returns null.

Parameters:
str - the string to reverse
Returns:
the reversed string

reverseArray

public static void reverseArray(java.lang.Object[] array)
Reverses an array. TAKEN FROM CollectionsUtils.


stackTrace

public static java.lang.String stackTrace(java.lang.Throwable throwable)
Get the stack trace from a Throwable as a String.

This method uses printStackTrace() internally to obtain the stack trace.

Parameters:
throwable - the throwable to extract a stack trace from
Returns:
the extracted stack trace, or null if an error occurs
See Also:
Throwable.printStackTrace()

convertUnicodeToNative

public static java.lang.String convertUnicodeToNative(java.lang.String source,
                                                      java.lang.String charset)
                                               throws java.io.IOException
Convert a string from unicode to bytes in a native encoding. The string must be in unicode (as Java always expects this); convertNativeToUnicode(String, String) will convert strings in native encodings into unicode. This method is generally used to create a String for use as output, and is useful when dealing with I18N.

Parameters:
source - String the unicode string to convert
charset - String the name of the charset into which to convert.
Returns:
The string given represented in the native encoding specified.
Throws:
java.io.IOException
See Also:
convertNativeToUnicode(String, String)

convertNativeToUnicode

public static java.lang.String convertNativeToUnicode(java.lang.String input,
                                                      java.lang.String charset)
                                               throws java.io.IOException
Convert a string from a native encoding to unicode. This method is generally used to create a String for use as input, and is useful when dealing with I18N.

Parameters:
input - String the input to convert from native encoding to unicode.
charset - String the charset from which to convert.
Returns:
The string given represented in unicode rather than the specified native encoding.
Throws:
java.io.IOException

main

public static void main(java.lang.String[] args)

reverseSort

public static java.lang.String[] reverseSort(java.lang.String[] members)
Sort by the tail-end first.

Parameters:
members - unsorted, possibly duplicated members
Returns:
still duplicated but sorted by the tail

sort

public static java.lang.String[] sort(java.lang.String[] members)
Sort by the front-end first.

Parameters:
members - unsorted, possibly duplicated members
Returns:
still duplicated but sorted by the tail