wyvern.common.util
Class Strings

java.lang.Object
  extended bywyvern.common.util.Strings

public class Strings
extends java.lang.Object

This class provides static utility methods for doing complex string processing.

Version:
1.0, Jun 03, 1998
Author:
Steve Yegge

Constructor Summary
Strings()
           
 
Method Summary
static java.lang.String capitalize(java.lang.String str)
          Ensures the given string starts with a capital letter.
static java.lang.String captureStackTrace(java.lang.Throwable t)
          Takes a Throwable and prints its stack trace into a string.
static byte[] compress(byte[] b, boolean useZLIB)
          Compresses a byte array with gzip.
static java.lang.String concat(java.lang.String[] strings)
          Concatenates the strings of a String array, using spaces.
static java.lang.String concat(java.lang.String[] strings, int index)
          Concatenates the strings of a String array, starting at the passed array index.
static java.lang.String concat(java.lang.String[] strings, int start, int end)
          Concatenates the strings of a String array, starting at the passed array index, and ending just before the end-index.
static java.lang.String concat(java.util.StringTokenizer st)
          Concatenates the remaining tokens from the passed StringTokenizer using spaces and returns the resulting string.
static boolean contains(java.lang.String[] array, java.lang.String s)
          Returns true if the passed String array contains the passed String.
static java.lang.String create(char c, int repeats)
          Creates a string by concatenating N of the same character.
static java.lang.String create(java.lang.String s, int repeats)
          Creates a string by concatenating N of the same string.
static java.lang.String formatDouble(double d)
          Returns a double formatted to 2 decimal places.
static java.lang.String formatInt(long number)
          Formats an integer with commas.
static java.lang.String formatStringArray(java.lang.String[] strings)
          Returns a printable version of a string array.
static boolean isAllCaps(java.lang.String s)
          Returns true if the passed string has no lowercase letters.
static boolean isCapitalized(java.lang.String s)
          Returns true if the first letter is a capital.
static boolean isVowel(char c)
          Returns true if the specified character is a vowel.
static void main(java.lang.String[] args)
          Tests some functions.
static java.lang.String makeAllCaps(java.lang.String s)
          Turns all lowercase letters into uppercase letters.
static int occurrencesOf(java.lang.String s, char c)
          Counts occurrences of the specified character in a string.
static java.lang.String pad(java.lang.String text, int size)
          Pads the passed string to N characters, appending spaces.
static java.lang.String percent(double num1, double num2)
          Returns a percentage formatted to two decimal places.
static void printArray(java.lang.Object[] objects)
          Prints an arbitrary array of objects, invoking toString on them.
static java.lang.String printField(java.lang.String s, int len)
          Creates a string of a fixed width containing the passed string.
static void printStringArray(java.lang.String[] strings)
          Prints a string array.
static java.lang.String readFile(java.lang.String path)
          Reads a text file into a single string.
static java.lang.String replaceString(java.lang.String s, java.lang.String orig, java.lang.String newstr)
          Replaces every occurrence of XX with YY in the passed string.
static java.lang.String replaceString(java.lang.String s, java.lang.String oldstr, java.lang.String newstr, boolean ignoreCase)
          Replaces every occurrence of XX with YY in the passed string.
static java.lang.String rpad(java.lang.String text, int size)
          Pads the passed string to N characters, prepending spaces, so that the string is right-justified.
static java.lang.String[] splitSubject(java.lang.String subject)
          Splits a dotted subject into an array of Strings, one per parent subject.
static java.lang.String substitute(java.lang.String text, java.lang.String oldstr, java.lang.String newstr)
          Performs a substitution of one substring for another in the passed String.
static java.lang.String substitute(java.lang.String s, java.lang.String oldstr, java.lang.String newstr, boolean ignoreCase)
          Performs a substitution of one substring for another in the passed String.
static java.lang.String[] tokenize(java.lang.String input)
          Splits the string with a StringTokenizer and puts the results into a string array.
static java.lang.String uncapitalize(java.lang.String str)
          Ensures the given string starts with a lowercase letter.
static byte[] uncompress(byte[] b)
          Uncompresses an array of bytes compressed with gzip.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Strings

public Strings()
Method Detail

occurrencesOf

public static int occurrencesOf(java.lang.String s,
                                char c)
Counts occurrences of the specified character in a string.

Parameters:
s - the string to examine
c - the character to look for
Returns:
the number of occurrences

concat

public static java.lang.String concat(java.util.StringTokenizer st)
Concatenates the remaining tokens from the passed StringTokenizer using spaces and returns the resulting string. Trims the string before returning it.

Parameters:
st - a StringTokenizer that may already have traversed some of its tokens. Only includes the remaining tokens.

concat

public static java.lang.String concat(java.lang.String[] strings)
Concatenates the strings of a String array, using spaces. Trims the output before returning it.


concat

public static java.lang.String concat(java.lang.String[] strings,
                                      int index)
Concatenates the strings of a String array, starting at the passed array index. Adds spaces between the strings and trims the output.

Parameters:
strings - the String array to concatenate
index - the starting index
Returns:
the concatenated string array, null if the passed array was null of the index was invalid.

concat

public static java.lang.String concat(java.lang.String[] strings,
                                      int start,
                                      int end)
Concatenates the strings of a String array, starting at the passed array index, and ending just before the end-index. Adds spaces between the strings and trims the output.

Parameters:
strings - a String array to concatenate
start - the starting index
end - the ending index (NOT inclusive).
Returns:
the concatenated string array, null if the passed array was null.

tokenize

public static java.lang.String[] tokenize(java.lang.String input)
Splits the string with a StringTokenizer and puts the results into a string array.

Parameters:
input - the space-delimited input string
Returns:
an array of the tokens, with whitespace discarded. If the input is null or all whitespace, the array will be of length zero.

substitute

public static java.lang.String substitute(java.lang.String text,
                                          java.lang.String oldstr,
                                          java.lang.String newstr)
Performs a substitution of one substring for another in the passed String. Only replaces the first match.

Parameters:
text - the main string on which to perform the replacement
oldstr - the substring to replace
newstr - the substring to replace with
Returns:
a new String with the substitution performed. If the substring to replace is not found, returns the original string.

substitute

public static java.lang.String substitute(java.lang.String s,
                                          java.lang.String oldstr,
                                          java.lang.String newstr,
                                          boolean ignoreCase)
Performs a substitution of one substring for another in the passed String. Only replaces the first match.

Parameters:
s - the main string on which to perform the replacement
oldstr - the substring to replace
newstr - the substring to replace with
ignoreCase - true if the replacement string should have the same case (lowercase, capitalized, or all-caps) as the string to replace.
Returns:
a new String with the substitution performed. If the substring to replace is not found, returns the original string.

replaceString

public static java.lang.String replaceString(java.lang.String s,
                                             java.lang.String orig,
                                             java.lang.String newstr)
Replaces every occurrence of XX with YY in the passed string.

Parameters:
s - the string on which to perform the replacement
orig - the substring to replace (XX)
newstr - the replacement substring (YY)
Returns:
the modified string

replaceString

public static java.lang.String replaceString(java.lang.String s,
                                             java.lang.String oldstr,
                                             java.lang.String newstr,
                                             boolean ignoreCase)
Replaces every occurrence of XX with YY in the passed string.

Parameters:
s - the string on which to perform the replacement
oldstr - the substring to replace (XX)
newstr - the replacement substring (YY)
ignoreCase - if true, then if the string to replace in the original is capitalized, the replacement string will be as well. And if the original substring is all-caps, the replacement will be made all-caps.
Returns:
the modified string

create

public static java.lang.String create(char c,
                                      int repeats)
Creates a string by concatenating N of the same character.


create

public static java.lang.String create(java.lang.String s,
                                      int repeats)
Creates a string by concatenating N of the same string. So create("quack ", 3) will produce "quack quack quack ".

Parameters:
s - the string to repeat
repeats - the number of times to concat s

pad

public static java.lang.String pad(java.lang.String text,
                                   int size)
Pads the passed string to N characters, appending spaces.

Parameters:
text - the string to pad
size - the length to return.
Returns:
If size parameter is less than text.length(), returns the text parameter (not truncated). Otherwise appends enough spaces to pad out to size parameter. If you want the string truncated if it's too long, use printField() instead.

rpad

public static java.lang.String rpad(java.lang.String text,
                                    int size)
Pads the passed string to N characters, prepending spaces, so that the string is right-justified.

Parameters:
text - the string to pad
size - the length to return.
Returns:
If size parameter is less than text.length(), returns the text parameter (not truncated). Otherwise, prepends enough spaces to pad out to size parameter.

capitalize

public static java.lang.String capitalize(java.lang.String str)
Ensures the given string starts with a capital letter.


uncapitalize

public static java.lang.String uncapitalize(java.lang.String str)
Ensures the given string starts with a lowercase letter.


isAllCaps

public static boolean isAllCaps(java.lang.String s)
Returns true if the passed string has no lowercase letters.


isCapitalized

public static boolean isCapitalized(java.lang.String s)
Returns true if the first letter is a capital.


makeAllCaps

public static java.lang.String makeAllCaps(java.lang.String s)
Turns all lowercase letters into uppercase letters.


isVowel

public static boolean isVowel(char c)
Returns true if the specified character is a vowel.


contains

public static final boolean contains(java.lang.String[] array,
                                     java.lang.String s)
Returns true if the passed String array contains the passed String.

Parameters:
array - a string array
s - a string
Returns:
true if the array contains the string

captureStackTrace

public static java.lang.String captureStackTrace(java.lang.Throwable t)
Takes a Throwable and prints its stack trace into a string.


printField

public static java.lang.String printField(java.lang.String s,
                                          int len)
Creates a string of a fixed width containing the passed string. If the passed string is longer than the field width, truncates the string, else pads the string with spaces.

Parameters:
s - the string to format
len - the field length
Returns:
the field string

readFile

public static java.lang.String readFile(java.lang.String path)
Reads a text file into a single string.

Parameters:
path - the absolute path to the file
Returns:
the contents as a string, or null if there was an error

compress

public static byte[] compress(byte[] b,
                              boolean useZLIB)
Compresses a byte array with gzip.

Parameters:
b - the array of bytes to compress
useZLIB - true to use ZLIB, false to use GZIP.
Returns:
the compressed array

uncompress

public static byte[] uncompress(byte[] b)
Uncompresses an array of bytes compressed with gzip.

Parameters:
b - a byte[] that was compressed with a DeflatorOutputStream
Returns:
the uncompressed data only used by client, so always use zlib from now on

formatStringArray

public static java.lang.String formatStringArray(java.lang.String[] strings)
Returns a printable version of a string array.


printStringArray

public static void printStringArray(java.lang.String[] strings)
Prints a string array.


printArray

public static void printArray(java.lang.Object[] objects)
Prints an arbitrary array of objects, invoking toString on them.


percent

public static java.lang.String percent(double num1,
                                       double num2)
Returns a percentage formatted to two decimal places.

Parameters:
num1 - the numerator
num2 - the denominator
Returns:
num1/num2 as a percentage

formatDouble

public static java.lang.String formatDouble(double d)
Returns a double formatted to 2 decimal places.

Parameters:
d - a number like "37.02459234"
Returns:
the number rounded to 2 decimal places

formatInt

public static java.lang.String formatInt(long number)
Formats an integer with commas.


splitSubject

public static java.lang.String[] splitSubject(java.lang.String subject)
Splits a dotted subject into an array of Strings, one per parent subject.

Parameters:
subject - a subject like "wyvern.player.init"
Returns:
an array of strings, one per parent path. In the example above, it returns {"wyvern.", "wyvern.player.", "wyvern.player.init" }.
Throws:
java.lang.NullPointerException - is the subject is null

main

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