com.mindprod.common11
Class StringTools

java.lang.Object
  extended by com.mindprod.common11.StringTools

public class StringTools
extends java.lang.Object

Miscellaneous static methods for dealing with Strings in JDK 1.1+.

Augmented by com.mindprod.common15.StringTools for JDK 1.5+.

Since:
2003-05-15
Version:
2.6 2010-02-11 - removed removeHead (redundant - same as chopLeadingString). renamed chopLeading to chopLeadingString to clearify does not trim a variety of chars.
Author:
Roedy Green, Canadian Mind Products

Constructor Summary
protected StringTools()
          Dummy constructor StringTools contains only static methods.
 
Method Summary
static void beep()
          makeshift system beep if awt.Toolkit.beep is not available.
static java.lang.String canonical(java.lang.String s)
          Convert String to canonical standard form.
static java.lang.String chopLeadingString(java.lang.String text, java.lang.String toChop)
          remove leading string if present
static java.lang.String chopTrailingString(java.lang.String text, java.lang.String toChop)
          remove trailing string if present
static java.lang.String condense(java.lang.String s)
          Collapse multiple spaces in string down to a single space.
static int countInstances(java.lang.String page, char lookFor)
          Count how many times a char occurs in a String.
static int countInstances(java.lang.String page, java.lang.String lookFor)
          Count how many times a String occurs on a page.
static int countLeading(java.lang.String text, char c)
          count of how many leading characters there are on a string matching a given character.
static int countLeading(java.lang.String text, java.lang.String possibleChars)
          count of how many leading characters there are on a string matching a given character.
static int countTrailing(java.lang.String text, char c)
          count of how many trailing characters there are on a string matching a given character.
static int countTrailing(java.lang.String text, java.lang.String possibleChars)
          count of how many trailing characters there are on a string matching a given character.
static java.lang.String firstWord(java.lang.String s)
          gets the first word of a String, delimited by space or the end of the string.
static boolean haveCommonChar(java.lang.String a, java.lang.String b)
          Returns true if strings a and b have one or more characters in common, not necessarily at the same offset.
static int indexOfWhiteSpace(java.lang.String s)
          find the first instance of whitespace (space, \n, \r, \t in a string.
static int indexOfWhiteSpace(java.lang.String s, int startOffset)
          find the first instance of whitespace (space, \n, \r, \t in a string.
static boolean isDigit(char c)
          Check if char is plain ASCII digit.
static boolean isEmpty(java.lang.String s)
          Is this string empty? In Java 1.6 + isEmpty is build in.
static boolean isLegal(char candidate, java.lang.String legalChars)
          Ensure the char is only one a set of legal characters.
static boolean isLegal(java.lang.String candidate, java.lang.String legalChars)
          Ensure the string contains only legal characters.
static boolean isLetter(char c)
          Check if char is plain ASCII letter lower or upper case.
static boolean isUnaccentedLowerCase(char c)
          Check if char is plain ASCII lower case.
static boolean isUnaccentedUpperCase(char c)
          Check if char is plain ASCII upper case.
static boolean isVowel(char c)
          is this character a vowel?
static java.lang.String lastWord(java.lang.String s)
          gets the last word of a String, delimited by space or the end of the string.
static java.lang.String leftJustified(int value, int newLen, boolean chop)
          Pads the string value out to the given length by applying blanks on the right, left justifying the value.
static java.lang.String leftPad(java.lang.String s, int newLen, boolean chop)
          Pads the string out to the given length by applying blanks on the left.
static void main(java.lang.String[] args)
          Test harness, used in debugging
static long parseDirtyLong(java.lang.String numStr)
          convert a String to a long.
static long parseLongPennies(java.lang.String numStr)
          convert a String into long pennies.
static java.lang.String penniesToString(long pennies)
          Print dollar currency, stored internally as scaled int.
static int pluck(java.lang.String s)
          Extracts a number from a string, returns 0 if malformed.
static java.lang.String[] pruneExcessBlankLines(java.lang.String[] lines, int minBlankLinesToKeep)
          Collapse multiple blank lines down to one.
static java.lang.String quoteSQL(java.lang.String sql)
          used to prepare SQL string literals by doubling each embedded ' and wrapping in ' at each end.
static java.lang.String rep(char c, int count)
          Produce a String of a given repeating character.
static java.lang.String rightJustified(int value, int newLen, boolean chop)
          Pads the string value out to the given length by applying blanks on the left, right justifying the value.
static java.lang.String rightPad(java.lang.String s, int newLen, boolean chop)
          Pads the string out to the given length by applying blanks on the right.
static java.lang.String spaces(int n)
          Generate a string of spaces n chars long.
static java.lang.String squish(java.lang.String s)
          Remove all spaces from a String.
static java.lang.String toBookTitleCase(java.lang.String s)
          convert to Book Title case, with first letter of each word capitalised.
static java.lang.String toHexString(int h)
          Convert int to hex with lead zeroes
static char toLowerCase(char c)
          Quick replacement for Character.toLowerCase for use with English-only.
static java.lang.String toLowerCase(java.lang.String s)
          Quick replacement for Character.toLowerCase for use with English-only.
static java.lang.String toLZ(int i, int len)
          Convert an integer to a String, with left zeroes.
static java.lang.String toLZHexString(int value, int len)
          convert an integer value to unsigned hex with leading zeroes.
static java.lang.String toString(java.awt.Color c)
          Get #ffffff html hex number for a colour
static char toUpperCase(char c)
          Quick replacement for Character.toUpperCase for use with English-only.
static java.lang.String toUpperCase(java.lang.String s)
          Quick replacement for Character.toUpperCase for use with English-only.
static java.lang.String trimLeading(java.lang.String s)
          Removes white space from beginning this string.
static java.lang.String trimLeading(java.lang.String text, char c)
          trim leading characters there are on a string matching a given character.
static java.lang.String trimLeading(java.lang.String text, java.lang.String toTrim)
          trim leading characters there are on a string matching a given characters
static java.lang.String trimTrailing(java.lang.String s)
          Removes white space from end this string.
static java.lang.String trimTrailing(java.lang.String text, char c)
          trim trailing characters there are on a string matching a given character.
static java.lang.String trimTrailing(java.lang.String text, java.lang.String toTrim)
          trim trailing characters there are on a string matching given characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringTools

protected StringTools()
Dummy constructor StringTools contains only static methods.

Method Detail

beep

public static void beep()
makeshift system beep if awt.Toolkit.beep is not available. Works also in JDK 1.02.


canonical

public static java.lang.String canonical(java.lang.String s)
Convert String to canonical standard form. null -> "". Trims lead trail blanks. Never null.

Parameters:
s - String to be converted.
Returns:
String in canonical form.

chopLeadingString

public static java.lang.String chopLeadingString(java.lang.String text,
                                                 java.lang.String toChop)
remove leading string if present

Parameters:
text - text with possible leading string, possibly empty or null.
toChop - the leading string of interest. Not a list of possible chars to chop, order matters.
Returns:
string with to toChop string removed if the text starts with it, otherwise the original string unmodified.
See Also:
trimLeading(String,String), chopTrailingString(String,String)

chopTrailingString

public static java.lang.String chopTrailingString(java.lang.String text,
                                                  java.lang.String toChop)
remove trailing string if present

Parameters:
text - text with possible trailing string, possibly empty, but not null.
toChop - the trailing string of interest. Not a list of possible chars to chop, order matters.
Returns:
string with to toChop string removed if the text ends with it, otherwise the original string unmodified.
See Also:
trimTrailing(String,String), chopLeadingString(String,String)

condense

public static java.lang.String condense(java.lang.String s)
Collapse multiple spaces in string down to a single space. Remove lead and trailing spaces. Does not collapse other whitespace.

Parameters:
s - String to strip of blanks.
Returns:
String with all blanks, lead/trail/embedded removed.
See Also:
squish(String)

countInstances

public static int countInstances(java.lang.String page,
                                 java.lang.String lookFor)
Count how many times a String occurs on a page.

Parameters:
page - big String to look in.
lookFor - small String to look for and count instances.
Returns:
number of times the String appears non-overlapping.

countInstances

public static int countInstances(java.lang.String page,
                                 char lookFor)
Count how many times a char occurs in a String.

Parameters:
page - big String to look in.
lookFor - char to lookfor count instances.
Returns:
number of times the char appears.

countLeading

public static int countLeading(java.lang.String text,
                               char c)
count of how many leading characters there are on a string matching a given character. It does not remove them.

Parameters:
text - text with possible leading characters, possibly empty, but not null.
c - the leading character of interest, usually ' ' or '\n'
Returns:
count of leading matching characters, possibly 0.

countLeading

public static int countLeading(java.lang.String text,
                               java.lang.String possibleChars)
count of how many leading characters there are on a string matching a given character. It does not remove them.

Parameters:
text - text with possible leading characters, possibly empty, but not null.
possibleChars - the leading characters of interest, usually ' ' or '\n'
Returns:
count of leading matching characters, possibly 0.

countTrailing

public static int countTrailing(java.lang.String text,
                                char c)
count of how many trailing characters there are on a string matching a given character. It does not remove them.

Parameters:
text - text with possible trailing characters, possibly empty, but not null.
c - the trailing character of interest, usually ' ' or '\n'
Returns:
count of trailing matching characters, possibly 0.

countTrailing

public static int countTrailing(java.lang.String text,
                                java.lang.String possibleChars)
count of how many trailing characters there are on a string matching a given character. It does not remove them.

Parameters:
text - text with possible trailing characters, possibly empty, but not null.
possibleChars - the trailing characters of interest, usually ' ' or '\n'
Returns:
count of trailing matching characters, possibly 0.

firstWord

public static java.lang.String firstWord(java.lang.String s)
gets the first word of a String, delimited by space or the end of the string. \n will not delimit a word. If there are no blanks in the string, the result is the entire string.

Parameters:
s - the input String
Returns:
the first word of the String.
See Also:
lastWord(String)

haveCommonChar

public static boolean haveCommonChar(java.lang.String a,
                                     java.lang.String b)
Returns true if strings a and b have one or more characters in common, not necessarily at the same offset. If you think of a and b as sets of chars, returns true if the intersection of those sets in not null. It can also me though of as like an indexOf that scans for multiple characters at once.

Parameters:
a - first string
b - second string
Returns:
true if the strings have one or more characters in common.

indexOfWhiteSpace

public static int indexOfWhiteSpace(java.lang.String s)
find the first instance of whitespace (space, \n, \r, \t in a string.

Parameters:
s - string to scan
Returns:
-1 if not found, offset relative to start of string where found

indexOfWhiteSpace

public static int indexOfWhiteSpace(java.lang.String s,
                                    int startOffset)
find the first instance of whitespace (space, \n, \r, \t in a string.

Parameters:
s - string to scan
startOffset - where in string to start looking
Returns:
-1 if not found, offset relative to start of string where found, not relative to startOffset.

isDigit

public static boolean isDigit(char c)
Check if char is plain ASCII digit.

Parameters:
c - char to check.
Returns:
true if char is in range 0-9
See Also:
Character.isLetter(char)

isEmpty

public static boolean isEmpty(java.lang.String s)
Is this string empty? In Java 1.6 + isEmpty is build in. Sun's version being an instance method cannot test for null.

Parameters:
s - String to be tested for emptiness.
Returns:
true if the string is null or equal to the "" null string. or just blanks

isLegal

public static boolean isLegal(java.lang.String candidate,
                              java.lang.String legalChars)
Ensure the string contains only legal characters.

Parameters:
candidate - string to test.
legalChars - characters than are legal for candidate.
Returns:
true if candidate is formed only of chars from the legal set.

isLegal

public static boolean isLegal(char candidate,
                              java.lang.String legalChars)
Ensure the char is only one a set of legal characters.

Parameters:
candidate - char to test.
legalChars - characters than are legal for candidate.
Returns:
true if candidate is one of the legallegal set.

isLetter

public static boolean isLetter(char c)
Check if char is plain ASCII letter lower or upper case.

Parameters:
c - char to check.
Returns:
true if char is in range a..z A..Z
See Also:
Character.isLowerCase(char), Character.isUpperCase(char), Character.isDigit(char)

isUnaccentedLowerCase

public static boolean isUnaccentedLowerCase(char c)
Check if char is plain ASCII lower case.

Parameters:
c - char to check.
Returns:
true if char is in range a..z.
See Also:
Character.isLowerCase(char), Character.isLetter(char)

isUnaccentedUpperCase

public static boolean isUnaccentedUpperCase(char c)
Check if char is plain ASCII upper case.

Parameters:
c - char to check.
Returns:
true if char is in range A..Z.
See Also:
Character.isUpperCase(char), Character.isLetter(char)

isVowel

public static boolean isVowel(char c)
is this character a vowel?

Parameters:
c - the character, any char upper or lower case, punctuation or symbol
Returns:
true if char is aeiou or AEIOU, or vowel accented in any way or ligature ae AE oe OE ij IJ

lastWord

public static java.lang.String lastWord(java.lang.String s)
gets the last word of a String, delimited by space or the end of the string.

Parameters:
s - the input String
Returns:
the last word of the String.
See Also:
firstWord(String)

leftJustified

public static java.lang.String leftJustified(int value,
                                             int newLen,
                                             boolean chop)
Pads the string value out to the given length by applying blanks on the right, left justifying the value.

Parameters:
value - value to be converted to string String to be padded/chopped.
newLen - length of new String desired.
chop - true if Strings longer than newLen should be truncated to newLen chars.
Returns:
String padded on right/chopped to the desired length. Spaces are inserted on the right.
See Also:
toLZ(int, int)

leftPad

public static java.lang.String leftPad(java.lang.String s,
                                       int newLen,
                                       boolean chop)
Pads the string out to the given length by applying blanks on the left.

Parameters:
s - String to be padded/chopped.
newLen - length of new String desired.
chop - true if Strings longer than newLen should be truncated to newLen chars.
Returns:
String padded on left/chopped to the desired length. Spaces are inserted on the left.
See Also:
toLZ(int, int)

parseDirtyLong

public static long parseDirtyLong(java.lang.String numStr)
convert a String to a long. The routine is very forgiving. It ignores invalid chars, lead trail, embedded spaces, decimal points etc. Dash is treated as a minus sign.

Parameters:
numStr - String to be parsed.
Returns:
long value of String with junk characters stripped.
Throws:
java.lang.NumberFormatException - if the number is too big to fit in a long.

parseLongPennies

public static long parseLongPennies(java.lang.String numStr)
convert a String into long pennies. It ignores invalid chars, lead trail, embedded spaces. Dash is treated as a minus sign. 0 or 2 decimal places are permitted.

Parameters:
numStr - String to be parsed.
Returns:
long pennies.
Throws:
java.lang.NumberFormatException - if the number is too big to fit in a long.

penniesToString

public static java.lang.String penniesToString(long pennies)
Print dollar currency, stored internally as scaled int. convert pennies to a string with a decorative decimal point.

Parameters:
pennies - long amount in pennies.
Returns:
amount with decorative decimal point, but no lead $.

pluck

public static int pluck(java.lang.String s)
Extracts a number from a string, returns 0 if malformed.

Parameters:
s - String containing the integer.
Returns:
binary integer.

pruneExcessBlankLines

public static java.lang.String[] pruneExcessBlankLines(java.lang.String[] lines,
                                                       int minBlankLinesToKeep)
Collapse multiple blank lines down to one. Discards lead and trail blank lines. Blank lines are lines that when trimmed have length 0. Enhanced version available in com.mindprod.common11.StringTools for JDK 1.5+.

Parameters:
lines - array of lines to tidy.
minBlankLinesToKeep - usually 1 meaning 1+ consecutive blank lines become 1, effectively collapsing runs of blank lines down to 1. if 2, 1 blank line is removed, and 2+ consecutive blanks lines become 1, effectively undouble spacing. if zero, non-blank lines will be separated by one blank line, even if there was not one there to begin with, completely independent of preexisting blank lines, effectively double spacing.. 9999 effectively removes all blank lines.
Returns:
array of lines with lead and trail blank lines removed, and excess blank lines collapsed down to one or 0. The results are NOT trimmed.

quoteSQL

public static java.lang.String quoteSQL(java.lang.String sql)
used to prepare SQL string literals by doubling each embedded ' and wrapping in ' at each end. Further quoting is required to use the results in Java String literals. If you use PreparedStatement, then this method is not needed. The ' quoting is automatically handled for you.

Parameters:
sql - Raw SQL string literal
Returns:
sql String literal enclosed in '

rep

public static java.lang.String rep(char c,
                                   int count)
Produce a String of a given repeating character.

Parameters:
c - the character to repeat
count - the number of times to repeat
Returns:
String, e.g. rep('*',4) returns "****"

rightJustified

public static java.lang.String rightJustified(int value,
                                              int newLen,
                                              boolean chop)
Pads the string value out to the given length by applying blanks on the left, right justifying the value.

Parameters:
value - value to be converted to string String to be padded/chopped.
newLen - length of new String desired.
chop - true if Strings longer than newLen should be truncated to newLen chars.
Returns:
String padded on left/chopped to the desired length. Spaces are inserted on the left.
See Also:
toLZ(int, int)

rightPad

public static java.lang.String rightPad(java.lang.String s,
                                        int newLen,
                                        boolean chop)
Pads the string out to the given length by applying blanks on the right.

Parameters:
s - String to be padded/chopped.
newLen - length of new String desired.
chop - true if Strings longer than newLen should be truncated to newLen chars.
Returns:
String padded on right/chopped to the desired length. Spaces are inserted on the right.

spaces

public static java.lang.String spaces(int n)
Generate a string of spaces n chars long.

Parameters:
n - how many spaces long
Returns:
a string of spaces n chars long.

squish

public static java.lang.String squish(java.lang.String s)
Remove all spaces from a String. Does not touch other whitespace.

Parameters:
s - String to strip of blanks.
Returns:
String with all blanks, lead/trail/embedded removed.
See Also:
condense(String)

toBookTitleCase

public static java.lang.String toBookTitleCase(java.lang.String s)
convert to Book Title case, with first letter of each word capitalised. e.g. "handbook to HIGHER consciousness" -> "Handbook to Higher Consciousness" e.g. "THE HISTORY OF THE U.S.A." -> "The History of the U.S.A." e.g. "THE HISTORY OF THE USA" -> "The History of the Usa" (sorry about that.) Don't confuse this with Character.isTitleCase which concerns ligatures.

Parameters:
s - String to convert. May be any mixture of case.
Returns:
String with each word capitalised, except embedded words "the" "of" "to"

toHexString

public static java.lang.String toHexString(int h)
Convert int to hex with lead zeroes

Parameters:
h - number you want to convert to hex
Returns:
0x followed by unsigned hex 8-digit representation
See Also:
toString(Color)

toLZ

public static java.lang.String toLZ(int i,
                                    int len)
Convert an integer to a String, with left zeroes.

Parameters:
i - the integer to be converted
len - the length of the resulting string. Warning. It will chop the result on the left if it is too long.
Returns:
String representation of the int e.g. 007
See Also:
leftPad(java.lang.String, int, boolean)

toLZHexString

public static java.lang.String toLZHexString(int value,
                                             int len)
convert an integer value to unsigned hex with leading zeroes.

Parameters:
value - integer to convert.
len - how many characters you want in the result.
Returns:
value in hex, padded to len chars with 0s on the left.

toLowerCase

public static char toLowerCase(char c)
Quick replacement for Character.toLowerCase for use with English-only. It does not deal with accented characters.

Parameters:
c - character to convert
Returns:
character converted to lower case

toLowerCase

public static java.lang.String toLowerCase(java.lang.String s)
Quick replacement for Character.toLowerCase for use with English-only. It does not deal with accented characters.

Parameters:
s - String to convert
Returns:
String converted to lower case

toString

public static java.lang.String toString(java.awt.Color c)
Get #ffffff html hex number for a colour

Parameters:
c - Color object whose html colour number you want as a string
Returns:
# followed by 6 hex digits
See Also:
toHexString(int)

toUpperCase

public static char toUpperCase(char c)
Quick replacement for Character.toUpperCase for use with English-only. It does not deal with accented characters.

Parameters:
c - character to convert
Returns:
character converted to upper case

toUpperCase

public static java.lang.String toUpperCase(java.lang.String s)
Quick replacement for Character.toUpperCase for use with English-only. It does not deal with accented characters.

Parameters:
s - String to convert
Returns:
String converted to upper case

trimLeading

public static java.lang.String trimLeading(java.lang.String s)
Removes white space from beginning this string.

Parameters:
s - String to process. As always the original in unchanged.
Returns:
this string, with leading white space removed
See Also:

All characters that have codes less than or equal to '\u0020' (the space character) are considered to be white space.


trimLeading

public static java.lang.String trimLeading(java.lang.String text,
                                           char c)
trim leading characters there are on a string matching a given character.

Parameters:
text - text with possible trailing characters, possibly empty, but not null.
c - the trailing character of interest, usually ' ' or '\n'
Returns:
string with any of those trailing characters removed.
See Also:
trimLeading(String)

trimLeading

public static java.lang.String trimLeading(java.lang.String text,
                                           java.lang.String toTrim)
trim leading characters there are on a string matching a given characters

Parameters:
text - text with possible trailing characters, possibly empty, but not null.
toTrim - the leading characters of interest, usually ' ' or '\n'
Returns:
string with any of those leading characters removed.
See Also:
trimTrailing(String), chopLeadingString(String,String)

trimTrailing

public static java.lang.String trimTrailing(java.lang.String s)
Removes white space from end this string.

Parameters:
s - String to process. As always the original in unchanged.
Returns:
this string, with trailing white space removed
See Also:

All characters that have codes less than or equal to '\u0020' (the space character) are considered to be white space.


trimTrailing

public static java.lang.String trimTrailing(java.lang.String text,
                                            char c)
trim trailing characters there are on a string matching a given character.

Parameters:
text - text with possible trailing characters, possibly empty, but not null.
c - the trailing character of interest, usually ' ' or '\n'
Returns:
string with any of those trailing characters removed.
See Also:
trimTrailing(String)

trimTrailing

public static java.lang.String trimTrailing(java.lang.String text,
                                            java.lang.String toTrim)
trim trailing characters there are on a string matching given characters.

Parameters:
text - text with possible trailing characters, possibly empty, but not null.
toTrim - the trailing characters of interest, usually ' ' or '\n'
Returns:
string with any of those trailing characters removed. ".com" would not only chop .com, but any combination of those letters e.g. mc.moc
See Also:
trimTrailing(String), chopTrailingString(String,String)

main

public static void main(java.lang.String[] args)
Test harness, used in debugging

Parameters:
args - not used