info.jonclark.util
Class StringUtils

java.lang.Object
  extended by info.jonclark.util.StringUtils

public class StringUtils
extends Object

A few utility functions for strings. Use of intern() is recommended so that equalities may be expressed as == instead of .equals()


Constructor Summary
StringUtils()
           
 
Method Summary
static int countOccurances(String searchable, char c)
          Count the occurances of substring within searchable
static int countOccurancesOfAnyDelim(String searchable, String delims)
          Count the occurances of any of the delims within searchable
static int countOccurancesOfSingleDelim(String searchable, String substring)
          Count the occurances of substring within searchable
static int countTokens(String str)
          Counts the space-delimited tokens in a string
static String cutCharsFromEnd(String in, int nCharsToCut)
          Remove a specified number of characters from the end of a string.
static String duplicateCharacter(char c, int nTimes)
          Returns a String of a character duplicated a given number of times.
static String escapeUnicode(String unicode)
          Replace all unicode characters in a string with their corresponding unicode escape sequences
static String forceNumberLength(String str, int nMinDigits)
          Forces a number (probably a hex or binary number) to have a certain number of digits.
static String getStackTrace(Throwable t)
          Get a String representation of a StackTrace from a Throwable object without being forced to write it to stderr.
static void internTokens(String[] tokens)
          Ensures that all elements of the array tokens is a member of Java's internal String pool by calling String.intern() on them.
static boolean rangesAreOrdered(Vector<IntRange> ranges)
          Returns true if all elements of ranges are in ascending order according to their each range's first element.
static boolean rangesDoNotOverlap(Vector<IntRange> ranges)
          Returns true if no range has a starting position <= to the previous range in the vector.
static String removeTrailingString(String target, String trailing)
          Removes a trailing string from a target string.
static String replaceFast(String target, String[] oldArr, String[] replacementArr)
           
static String replaceFast(String target, String old, String replacement)
          Replaces all occurances of old with replacement within target.
static String replaceFast(String target, Vector<IntRange> oldArr, Vector<String> replacementArr)
          Replaces each string at oldArr[i] with replacementArr[i] within target.
static String substringAfter(String in, String delim)
          Get the substring of in that occurs after the string delim
static String substringAfter(String in, String delim, boolean returnDelims)
          Get the substring of in that occurs after the string delim
static String substringBefore(String in, String delim)
          Get the substring of in that occurs before the string delim
static String substringBefore(String in, String delim, boolean returnDelims)
          Get the substring of in that occurs before the string delim
static String[] tokenize(String str)
          Create an array of tokens given a String separated delimited by spaces.
static String[] tokenize(String str, String delims)
          Tokenize a string delimited by any of a given set of single-character delimiters.
static String[] tokenize(String str, String delims, int nMaxSplits)
           
static String untokenize(String[] tokens)
          Create a single string from an array of tokens, adding a space between each token.
static String untokenize(String[] tokens, int nStartElement)
          Create a single string from an array of tokens, starting with the element having index nStartElement adding the specified string delim between each token.
static String untokenize(String[] tokens, int nStartElement, int nLastElement)
          Create a single string from an array of tokens, starting with the element having index nStartElement and ending with nLastElement inclusive, adding the specified string delim between each token.
static String untokenize(String[] tokens, String delim)
          Create a single string from an array of tokens, adding the specified string delim between each token.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

public StringUtils()
Method Detail

escapeUnicode

public static String escapeUnicode(String unicode)
Replace all unicode characters in a string with their corresponding unicode escape sequences

Parameters:
unicode - A string possibly containing unicode characters
Returns:
A string having all ASCII characters with all unicode characters from unicode replaced with their escape sequences

forceNumberLength

public static String forceNumberLength(String str,
                                       int nMinDigits)
Forces a number (probably a hex or binary number) to have a certain number of digits. If a number has less than the required number of digits, leading zeros are prepended.

Parameters:
str - The number string
nMinDigits - The minimum number of digits in the output string
Returns:
The inputted number with leading zeros prepended, if necessary

duplicateCharacter

public static String duplicateCharacter(char c,
                                        int nTimes)
Returns a String of a character duplicated a given number of times.

Parameters:
c - The character.
nTimes - The number of times it should be duplicated.
Returns:
A string of length nTimes containing only the character c.

rangesAreOrdered

public static boolean rangesAreOrdered(Vector<IntRange> ranges)
Returns true if all elements of ranges are in ascending order according to their each range's first element.

Parameters:
ranges -
Returns:

rangesDoNotOverlap

public static boolean rangesDoNotOverlap(Vector<IntRange> ranges)
Returns true if no range has a starting position <= to the previous range in the vector. This method should be used in conjunction with rangesAreOrdered() to guarantee no overlaps. That is, this method does NOT check for all possible combination of overlaps by itself.

Parameters:
ranges -
Returns:

removeTrailingString

public static String removeTrailingString(String target,
                                          String trailing)
Removes a trailing string from a target string.

e.g. Removing the traling string "xyz" from "abcxyz" produces "abc".

Parameters:
target - The target string, possibly containing the trailing string.
trailing - The trailing string to be removed.
Returns:
The target minus the trailing string, if it is found. Otherwise, the target string is returned.

replaceFast

public static String replaceFast(String target,
                                 String[] oldArr,
                                 String[] replacementArr)

replaceFast

public static String replaceFast(String target,
                                 Vector<IntRange> oldArr,
                                 Vector<String> replacementArr)
Replaces each string at oldArr[i] with replacementArr[i] within target. Note that oldArr.size() and replacementArr.size() must be equal. Also, the ranges in oldArr must be in ascending order and must not contain any overlapping ranges.

FYI: This method was written with tokenization in mind. This allows all regions that are to be tokenized to be replaced at one time, thus requiring only a single buffer reallocation.

Parameters:
target - The string on which the replace operation will be performed
oldArr - The strings that will be replaced
replacementArr - The strings that will be substituted for old
Returns:
The string with all replacements made

replaceFast

public static String replaceFast(String target,
                                 String old,
                                 String replacement)
Replaces all occurances of old with replacement within target.

Parameters:
target - The string on which the replace operation will be performed
old - The strings that will be replaced
replacement - The strings that will be substituted for old
Returns:
The string with all replacements made

countOccurancesOfSingleDelim

public static int countOccurancesOfSingleDelim(String searchable,
                                               String substring)
Count the occurances of substring within searchable

Parameters:
searchable - The string containing zero or more occurances of substring
substring - The substring that we hope to find
Returns:

countOccurancesOfAnyDelim

public static int countOccurancesOfAnyDelim(String searchable,
                                            String delims)
Count the occurances of any of the delims within searchable

Parameters:
searchable - The string containing zero or more occurances of one of the delims
delims - A string containing multiple single-character delimiters, any of which could be a
Returns:

countTokens

public static int countTokens(String str)
Counts the space-delimited tokens in a string

Parameters:
str -
Returns:

countOccurances

public static int countOccurances(String searchable,
                                  char c)
Count the occurances of substring within searchable

Parameters:
searchable - The string containing zero or more occurances of a character c
c - The character that we hope to find
Returns:

substringAfter

public static String substringAfter(String in,
                                    String delim)
Get the substring of in that occurs after the string delim

Parameters:
in - The string (possibly) containing delim
delim - The string (possibly) contained in in
Returns:
The string after the delimiter if the delimiter was found, otherwise the original string

substringAfter

public static String substringAfter(String in,
                                    String delim,
                                    boolean returnDelims)
Get the substring of in that occurs after the string delim

Parameters:
in - The string (possibly) containing delim
delim - The string (possibly) contained in in
returnDelims - Should the delimiter delim be part of the returned String?
Returns:
The string after the delimiter if the delimiter was found, otherwise the original string

substringBefore

public static String substringBefore(String in,
                                     String delim)
Get the substring of in that occurs before the string delim

Parameters:
in - The string (possibly) containing delim
delim - The string (possibly) contained in in
Returns:
The string before the delimiter if the delimiter was found, otherwise the original string

substringBefore

public static String substringBefore(String in,
                                     String delim,
                                     boolean returnDelims)
Get the substring of in that occurs before the string delim

Parameters:
in - The string (possibly) containing delim
delim - The string (possibly) contained in in
returnDelims - Should the delimiter delim be part of the returned String?
Returns:
The string before the delimiter if the delimiter was found, otherwise the original string

tokenize

public static String[] tokenize(String str)
Create an array of tokens given a String separated delimited by spaces.

Parameters:
str - The string to be tokenized.
Returns:
An array with the tokens produced from str.

tokenize

public static String[] tokenize(String str,
                                String delims)
Tokenize a string delimited by any of a given set of single-character delimiters. For instance, "A.B,C!D" delimited by ".,!" would produce the array [A,B,C,D]

Parameters:
str - The string to be tokenized.
delims - The set of single-character delimiters to be used in tokenizing.
Returns:
The array of tokens produced from str.

tokenize

public static String[] tokenize(String str,
                                String delims,
                                int nMaxSplits)
Parameters:
str -
delims -
nMaxSplits - The maximum size of the returned array. Even if not all delimiters have been exhaustyed, the last element of the array will contain the remaining portion of the input string.
Returns:

internTokens

public static void internTokens(String[] tokens)
Ensures that all elements of the array tokens is a member of Java's internal String pool by calling String.intern() on them.

Parameters:
tokens - The array for which all member Strings will be pooled.

untokenize

public static String untokenize(String[] tokens)
Create a single string from an array of tokens, adding a space between each token.

Parameters:
tokens - The array of tokens to be untokenized.
Returns:
The String form of the token array.

untokenize

public static String untokenize(String[] tokens,
                                String delim)
Create a single string from an array of tokens, adding the specified string delim between each token.

Parameters:
tokens - The array of tokens to be untokenized.
delim - The string that will be placed between each token.
Returns:
The String form of the token array.

untokenize

public static String untokenize(String[] tokens,
                                int nStartElement)
Create a single string from an array of tokens, starting with the element having index nStartElement adding the specified string delim between each token.

Parameters:
tokens - The array of tokens to be untokenized.
nStartElement - The array index of the first element that will be included in the output string.
Returns:
The String form of the given part of the token array.

untokenize

public static String untokenize(String[] tokens,
                                int nStartElement,
                                int nLastElement)
Create a single string from an array of tokens, starting with the element having index nStartElement and ending with nLastElement inclusive, adding the specified string delim between each token.

Parameters:
tokens - The array of tokens to be untokenized.
nStartElement - The array index of the first element that will be included in the output string.
nLastElement - The array index of the last element that will be included in the output string.
Returns:
The String form of the given part of the token array.

cutCharsFromEnd

public static String cutCharsFromEnd(String in,
                                     int nCharsToCut)
Remove a specified number of characters from the end of a string. Functions much the same as String.substring(n) except that it works from the right side of the string backward.

Parameters:
in - The string that will have characters removed from it
nCharsToCut - The number of characters that will be removed
Returns:

getStackTrace

public static String getStackTrace(Throwable t)
Get a String representation of a StackTrace from a Throwable object without being forced to write it to stderr.

Parameters:
t - The Throwable object containing the stack trace
Returns:
A string representation of the StackTrace