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.4 2009-04-30 - fix but in countLeading ( String text, String leads ). add pruneExcessBlankLines
Author:
Roedy Green, Canadian Mind Products

Constructor Summary
protected StringTools()
          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 chopLeading(java.lang.String text, java.lang.String toChop)
          trim leading string if present
static java.lang.String chopTrailing(java.lang.String text, java.lang.String toChop)
          trim 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 leads)
          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 trailing)
          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 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 isEmpty(java.lang.String s)
          Is this string empty? In Java 1.6 + isEmpty is build in.
static boolean isLegal(java.lang.String candidate, java.lang.String legalChars)
          Ensure the string contains only legal characters.
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 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 removeHead(java.lang.String s, java.lang.String head)
          Remove an unwanted bit of text at the head end of a string if it is present.
static java.lang.String removeTail(java.lang.String s, java.lang.String tail)
          Remove an unwanted bit of text at the tail end of a string if it is present.
static java.lang.String rep(char c, int count)
          Produce a String of a given repeating character.
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()
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.

chopLeading

public static java.lang.String chopLeading(java.lang.String text,
                                           java.lang.String toChop)
trim leading string if present

Parameters:
text - text with possible seading string, possibly empty, but not null.
toChop - the leading string of interest.
Returns:
string with to toChop string removed if the text starts with it, otherwise the original string unmodified.
See Also:
trimLeading(String,String), chopTrailing(String,String)

chopTrailing

public static java.lang.String chopTrailing(java.lang.String text,
                                            java.lang.String toChop)
trim trailing string if present

Parameters:
text - text with possible trailing string, possibly empty, but not null.
toChop - the trailing string of interest.
Returns:
string with to toChop string removed if the text ends with it, otherwise the original string unmodified.
See Also:
trimTrailing(String,String), chopLeading(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 leads)
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.
leads - 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 trailing)
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.
trailing - 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)

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.

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.

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)

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)

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)

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

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 '

removeHead

public static java.lang.String removeHead(java.lang.String s,
                                          java.lang.String head)
Remove an unwanted bit of text at the head end of a string if it is present. Case sensitive.

Parameters:
s - the string with the possibly wanted head. Will not be modified.
head - the characters you want to remove from the start of the string s if they are present.
Returns:
s with the head removed if it is present, otherwise s unmodified.

removeTail

public static java.lang.String removeTail(java.lang.String s,
                                          java.lang.String tail)
Remove an unwanted bit of text at the tail end of a string if it is present. Case sensitive.

Parameters:
s - the string with the possibly wanted tail. Will not be modified.
tail - the characters you want to remove from the end if they are present.
Returns:
s with the tail removed if it is present, otherwise s unmodified.

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 "****"

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)

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

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.

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), chopLeading(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), chopTrailing(String,String)

main

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

Parameters:
args - not used