|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectinfo.jonclark.util.StringUtils
public class StringUtils
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 |
|---|
public StringUtils()
| Method Detail |
|---|
public static String escapeUnicode(String unicode)
unicode - A string possibly containing unicode characters
unicode replaced with their
escape sequences
public static String forceNumberLength(String str,
int nMinDigits)
str - The number stringnMinDigits - The minimum number of digits in the output string
public static String duplicateCharacter(char c,
int nTimes)
c - The character.nTimes - The number of times it should be duplicated.
public static boolean rangesAreOrdered(Vector<IntRange> ranges)
true if all elements of ranges
are in ascending order according to their each range's first element.
ranges -
public static boolean rangesDoNotOverlap(Vector<IntRange> ranges)
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.
ranges -
public static String removeTrailingString(String target,
String trailing)
e.g. Removing the traling string "xyz" from "abcxyz" produces "abc".
target - The target string, possibly containing the trailing
string.trailing - The trailing string to be removed.
public static String replaceFast(String target,
String[] oldArr,
String[] replacementArr)
public static String replaceFast(String target,
Vector<IntRange> oldArr,
Vector<String> replacementArr)
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.
target - The string on which the replace operation will be
performedoldArr - The strings that will be replacedreplacementArr - The strings that will be substituted for
old
public static String replaceFast(String target,
String old,
String replacement)
old with
replacement within target.
target - The string on which the replace operation will be
performedold - The strings that will be replacedreplacement - The strings that will be substituted for
old
public static int countOccurancesOfSingleDelim(String searchable,
String substring)
substring within
searchable
searchable - The string containing zero or more occurances of
substringsubstring - The substring that we hope to find
public static int countOccurancesOfAnyDelim(String searchable,
String delims)
delims within
searchable
searchable - The string containing zero or more occurances of one
of the delimsdelims - A string containing multiple single-character
delimiters, any of which could be a
public static int countTokens(String str)
str -
public static int countOccurances(String searchable,
char c)
substring within
searchable
searchable - The string containing zero or more occurances of a
character cc - The character that we hope to find
public static String substringAfter(String in,
String delim)
in that occurs after the string
delim
in - The string (possibly) containing delimdelim - The string (possibly) contained in in
public static String substringAfter(String in,
String delim,
boolean returnDelims)
in that occurs after the string
delim
in - The string (possibly) containing delimdelim - The string (possibly) contained in inreturnDelims - Should the delimiter delim be part of
the returned String?
public static String substringBefore(String in,
String delim)
in that occurs before the string
delim
in - The string (possibly) containing delimdelim - The string (possibly) contained in in
public static String substringBefore(String in,
String delim,
boolean returnDelims)
in that occurs before the string
delim
in - The string (possibly) containing delimdelim - The string (possibly) contained in inreturnDelims - Should the delimiter delim be part of
the returned String?
public static String[] tokenize(String str)
str - The string to be tokenized.
public static String[] tokenize(String str,
String delims)
str - The string to be tokenized.delims - The set of single-character delimiters to be used in
tokenizing.
public static String[] tokenize(String str,
String delims,
int nMaxSplits)
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.
public static void internTokens(String[] tokens)
tokens is a
member of Java's internal String pool by calling String.intern() on
them.
tokens - The array for which all member Strings will be pooled.public static String untokenize(String[] tokens)
tokens - The array of tokens to be untokenized.
public static String untokenize(String[] tokens,
String delim)
delim between each token.
tokens - The array of tokens to be untokenized.delim - The string that will be placed between each token.
public static String untokenize(String[] tokens,
int nStartElement)
nStartElement adding the
specified string delim between each token.
tokens - The array of tokens to be untokenized.nStartElement - The array index of the first element that will be
included in the output string.
public static String untokenize(String[] tokens,
int nStartElement,
int nLastElement)
nStartElement and ending with
nLastElement inclusive, adding the specified string
delim between each token.
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.
public static String cutCharsFromEnd(String in,
int nCharsToCut)
in - The string that will have characters removed from itnCharsToCut - The number of characters that will be removed
public static String getStackTrace(Throwable t)
t - The Throwable object containing the
stack trace
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||