|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.northwestern.at.utils.StringUtils
public class StringUtils
String utilities.
This static class provides various utility methods for manipulating strings.
Constructor Summary | |
---|---|
protected |
StringUtils()
Don't allow instantiation, do allow overrides. |
Method Summary | |
---|---|
static java.lang.String |
areOrAreNot(boolean bool)
Return "are", "are not" based upon boolean value. |
static int |
balPos(java.lang.StringBuffer s,
int start,
char lChar,
char rChar)
Find end position of balanced string. |
static boolean |
checkEmpty(java.lang.String s)
Checks if a string is null or empty. |
static int |
compare(java.util.Date d1,
java.util.Date d2)
Compares two dates. |
static int |
compare(int n1,
int n2)
Compares two ints. |
static int |
compare(long n1,
long n2)
Compares two longs. |
static int |
compare(java.lang.String s1,
java.lang.String s2)
Compares two case-sensitive strings. |
static int |
compareIgnoreCase(java.lang.String s1,
java.lang.String s2)
Compares two case-insensitive strings. |
static java.lang.String |
compressMultipleOccurrences(java.lang.String s,
char c)
Compress multiple instances of a character in a string. |
static boolean |
containsIgnoreCaseWholeWord(java.lang.String str1,
java.lang.String str2)
Returns true if a string contains another case-insensitive string as a whole word. |
static int |
countChar(java.lang.StringBuffer sb,
char ch)
Count occurrences of character in string buffer. |
static int |
countOccurrences(java.lang.String source,
java.lang.String searchString)
Count occurrences of string in another string. |
static java.lang.String |
deleteChar(java.lang.String str,
char ch)
Delete all occurrences of a specified character from a string. |
static java.lang.String |
deleteParenthesizedText(java.lang.String s)
Deletes parenthesized text from a string. |
static java.lang.String |
deleteUnparenthesizedText(java.lang.String s)
Deletes unparenthesized text from a string. |
static java.lang.String |
displayBooleanField(java.lang.String name,
boolean value)
Displays a boolean field. |
static java.lang.String |
displayDoubleField(java.lang.String name,
double value)
Displays a double field. |
static java.lang.String |
displayIntegerField(java.lang.String name,
int value)
Displays an integer field. |
static java.lang.String |
displayStringField(java.lang.String name,
java.lang.String value)
Displays a string field. |
static java.lang.String |
dupl(char ch,
int n)
Duplicate character into string. |
static java.lang.String |
dupl(java.lang.String s,
int n)
Duplicate string into string. |
static boolean |
equals(java.lang.String s1,
java.lang.String s2)
Returns true if two case-sensitive strings are equal. |
static boolean |
equalsIgnoreCase(java.lang.String s1,
java.lang.String s2)
Returns true if two case-insensitive strings are equal. |
static java.lang.String |
escapeSpecialCharacters(java.lang.String s)
Escape special characters. |
static int |
extractNumber(java.lang.String str,
int pos)
Extracts a number from a string. |
static java.lang.String |
firstLine(java.lang.String str)
Returns the first line of a string. |
static java.lang.String |
formatNumberWithCommas(int n)
Formats a number with commas. |
static java.lang.String |
formatNumberWithCommas(long n)
Formats a number with commas. |
static java.lang.String |
getFirstTokenFromBytes(byte[] bytes,
int nBytes)
Gets the first token from an array of bytes. |
static java.lang.String |
haveOrHaveNot(boolean bool)
Return "have", "have not" based upon boolean value. |
static int |
indexOfIgnoreCaseWholeWord(java.lang.String str1,
java.lang.String str2,
int fromIndex)
Searches a string for a case-insensitive whole word substring match. |
static java.lang.String |
intersperseTextLines(java.lang.String s1,
java.lang.String s2,
java.lang.String s1Prefix,
java.lang.String s1Suffix,
java.lang.String s2Prefix,
java.lang.String s2Suffix)
Intersperse text lines from two files. |
static java.lang.String |
intToString(int intValue)
Convert integer to string. |
static java.lang.String |
intToStringWithZeroFill(int intValue,
int width)
Convert integer to string with left zero fill. |
static java.lang.String |
isOrIsNot(boolean bool)
Return "is", "is not" based upon boolean value. |
static boolean |
isRegularExpression(java.lang.String s)
See if string is a regular expression. |
static java.lang.String |
longToString(long longValue)
Convert long to string. |
static java.lang.String |
longToStringWithZeroFill(long longValue,
int width)
Convert long to string with left zero fill. |
static java.lang.String |
lpad(java.lang.String s,
int width)
Left pad string with blanks to specified width. |
static java.lang.String[] |
makeTokenArray(java.lang.String source)
Break up a string into an array of string tokens. |
static java.lang.String[] |
makeTokenArray(java.lang.String source,
java.lang.String delim)
Break up a string into an array of string tokens. |
static java.lang.String |
objectToString(java.lang.Object object)
Convert object to string representation. |
static int |
parseNumberWithCommas(java.lang.String str)
Parses a number with commas. |
static java.lang.String |
pluralize(int count,
java.lang.String singular,
java.lang.String plural)
Select singular or plural string based upon count. |
static java.lang.String |
removeEnclosingBrackets(java.lang.String str)
Remove enclosing brackets from a string. |
static java.lang.String |
replaceAll(java.lang.String str,
java.lang.String s,
java.lang.String r)
Replaces all substrings of a string. |
static java.lang.String |
rpad(java.lang.String s,
int width)
Right pad string with blanks to specified width. |
static java.lang.String |
safeString(java.lang.String s)
Makes possibly null string safe for comparisons. |
static java.lang.String |
stringToHexString(java.lang.String s)
Convert characters in a string to hex format. |
static int |
stringToInt(java.lang.String strValue)
Convert string to integer. |
static int |
stringToInt(java.lang.String strValue,
int defaultValue)
Convert string to integer. |
static long |
stringToLong(java.lang.String strValue)
Convert string to long. |
static long |
stringToLong(java.lang.String strValue,
long defValue)
Convert string to long. |
static java.lang.String |
trim(java.lang.String s)
Trims a string. |
static java.lang.String |
truncate(java.lang.String str,
int n)
Truncates a string and appends an ellipsis. |
static java.lang.String |
upperCaseFirstChar(java.lang.String str)
Converts the first character of a string to upper case. |
static java.lang.String |
wrapLine(java.lang.String line,
java.lang.String newline,
int wrapColumn)
Wrap one line of text. |
static java.lang.String |
wrapText(java.lang.String inString,
java.lang.String newline,
int wrapColumn)
Lines wraps a block of text. |
static java.lang.String |
yesNo(boolean yesno)
Returns "yes" or "no" for boolean value. |
static java.lang.String |
zeroPad(java.lang.String s,
int length)
Pad string with leading zeros. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected StringUtils()
Method Detail |
---|
public static int balPos(java.lang.StringBuffer s, int start, char lChar, char rChar)
s
- The string.start
- Starting offset in string.lChar
- Left delimiter.rChar
- Right delimiter to match left delimiter.
public static int countChar(java.lang.StringBuffer sb, char ch)
sb
- The string buffer.ch
- The character.
public static int countOccurrences(java.lang.String source, java.lang.String searchString)
source
- The source string.searchString
- The string for which to search.
public static java.lang.String deleteChar(java.lang.String str, char ch)
str
- The string.ch
- The character to delete.
public static java.lang.String displayBooleanField(java.lang.String name, boolean value)
name
- The field name.value
- The field value.
public static java.lang.String displayIntegerField(java.lang.String name, int value)
name
- The field name.value
- The field value.
public static java.lang.String displayDoubleField(java.lang.String name, double value)
name
- The field name.value
- The field value.
public static java.lang.String displayStringField(java.lang.String name, java.lang.String value)
name
- The field name.value
- The field value.
public static java.lang.String dupl(char ch, int n)
ch
- The character to be duplicated.n
- The number of duplicates desired.
if n <= 0, the empty string "" is returned.
public static java.lang.String dupl(java.lang.String s, int n)
s
- The string to be duplicated.n
- The number of duplicates desired.
if n <= 0, the empty string "" is returned.
public static boolean equals(java.lang.String s1, java.lang.String s2)
Nulls are permitted and are equal only to themselves.
s1
- String 1.s2
- String 2.
public static boolean equalsIgnoreCase(java.lang.String s1, java.lang.String s2)
Nulls are permitted and are equal only to themselves.
s1
- String 1.s2
- String 2.
public static int compare(java.lang.String s1, java.lang.String s2)
Nulls are permitted and are less than non-nulls.
s1
- String 1.s2
- String 2.
public static int compareIgnoreCase(java.lang.String s1, java.lang.String s2)
Nulls are permitted and are less than non-nulls.
s1
- String 1.s2
- String 2.
public static int compare(java.util.Date d1, java.util.Date d2)
Nulls are permitted and are less than non-nulls.
d1
- Date 1.d2
- Date 2.
public static int compare(int n1, int n2)
n1
- Int 1.n2
- Int 2.
public static int compare(long n1, long n2)
n1
- Long 1.n2
- Long 2.
public static int indexOfIgnoreCaseWholeWord(java.lang.String str1, java.lang.String str2, int fromIndex)
str1
- The string to be searched.str2
- The substring to search for, converted to lower case.fromIndex
- The index to start the search from.
public static boolean containsIgnoreCaseWholeWord(java.lang.String str1, java.lang.String str2)
str1
- The string to be searched.str2
- The substring to search for.
public static int extractNumber(java.lang.String str, int pos) throws java.text.ParseException
str
- The string.pos
- The position within the string.
java.text.ParseException
- If invalid number. public static java.lang.String getFirstTokenFromBytes(byte[] bytes, int nBytes)
bytes
- The bytes.nBytes
- The number of bytes to scan.
The first token is defined to end at a space, a tab, a carriage return, or a linefeed.
public static java.lang.String firstLine(java.lang.String str)
str
- The string.
public static java.lang.String formatNumberWithCommas(int n)
n
- The number (int).
public static java.lang.String formatNumberWithCommas(long n)
n
- The number (long).
public static int parseNumberWithCommas(java.lang.String str) throws java.text.ParseException
str
- The string.
java.text.ParseException
- Invalid number. public static java.lang.String safeString(java.lang.String s)
s
- Input string which may be null.
public static java.lang.String replaceAll(java.lang.String str, java.lang.String s, java.lang.String r)
Note: Not needed in Java 1.4 - can use replaceAll method of String class instead.
str
- The string.s
- Substring to be replaced.r
- Replacement string.
public static boolean checkEmpty(java.lang.String s)
s
- String to be checked.
public static java.lang.String yesNo(boolean yesno)
yesno
- Boolean value to be checked.
public static java.lang.String areOrAreNot(boolean bool)
bool
- Boolean value.
public static java.lang.String isOrIsNot(boolean bool)
bool
- Boolean value.
public static java.lang.String haveOrHaveNot(boolean bool)
bool
- Boolean value.
public static int stringToInt(java.lang.String strValue, int defaultValue)
strValue
- The string to convert.defaultValue
- Default value if conversion error occurs.
public static java.lang.String stringToHexString(java.lang.String s)
s
- The string to convert to hex.
public static java.lang.String[] makeTokenArray(java.lang.String source, java.lang.String delim)
source
- The source string.delim
- The delimiter.
public static java.lang.String[] makeTokenArray(java.lang.String source)
source
- The source string.
public static java.lang.String objectToString(java.lang.Object object)
object
- The object to be converted to a string representation.
The regular java toString() routine for arrays is brain-dead: it returns the hashcode/address of the array rather than a useful representation of the array contents.
public static java.lang.String pluralize(int count, java.lang.String singular, java.lang.String plural)
count
- The number of items.singular
- The output string iif count==1 .plural
- The output string iif count!=1 .
public static java.lang.String intToString(int intValue)
intValue
- The integer to convert.
public static java.lang.String intToStringWithZeroFill(int intValue, int width)
intValue
- The integer to convert.width
- Width of result field.
public static java.lang.String longToString(long longValue)
longValue
- The long to convert.
public static java.lang.String longToStringWithZeroFill(long longValue, int width)
longValue
- The long to convert.width
- Width of result field.
public static java.lang.String lpad(java.lang.String s, int width)
s
- The string to pad.width
- The width to pad to.
public static java.lang.String rpad(java.lang.String s, int width)
s
- The string to pad.width
- The width to pad to.
public static int stringToInt(java.lang.String strValue) throws java.lang.NumberFormatException
strValue
- The string to convert.
java.lang.NumberFormatException
public static long stringToLong(java.lang.String strValue) throws java.lang.NumberFormatException
strValue
- The string to convert.
java.lang.NumberFormatException
public static long stringToLong(java.lang.String strValue, long defValue)
strValue
- The string to convert.defValue
- Default value to return if string cannot be converted.
public static java.lang.String wrapLine(java.lang.String line, java.lang.String newline, int wrapColumn)
line
- A line which is in need of word-wrapping.newline
- The characters that define a newline.wrapColumn
- The column to wrap the words at.
public static java.lang.String wrapText(java.lang.String inString, java.lang.String newline, int wrapColumn)
inString
- Text which is in need of word-wrapping.newline
- The characters that define a newline.wrapColumn
- The column to wrap the words at.
This method wraps long lines based on the supplied wrapColumn parameter. Note: Remove or expand tabs before calling this method.
public static java.lang.String trim(java.lang.String s)
s
- The string.
public static java.lang.String zeroPad(java.lang.String s, int length)
s
- String to pad.length
- Length to pad to.
public static java.lang.String intersperseTextLines(java.lang.String s1, java.lang.String s2, java.lang.String s1Prefix, java.lang.String s1Suffix, java.lang.String s2Prefix, java.lang.String s2Suffix)
s1
- First string containing text lines.s2
- Second string containing text lines.s1Prefix
- Prefix for first string lines.s1Suffix
- Suffix for first string lines.s2Prefix
- Prefix for second string lines.s2Suffix
- Suffix for second string lines.
public static java.lang.String deleteParenthesizedText(java.lang.String s)
s
- The string from which to remove parenthesized text.
Example:
deleteParenthesizedText( "aaaa (bb) ccc (ddd) e" )
yields
"aaaa ccc e"
public static java.lang.String deleteUnparenthesizedText(java.lang.String s)
s
- The string from which to remove unparenthesized text.
Example:
deleteUnparenthesizedText( "aaaa (bb) ccc (ddd) e" )
yields
"bb ddd"
public static java.lang.String compressMultipleOccurrences(java.lang.String s, char c)
s
- The string in which to compress multiple occurrences.c
- Character whose multiple occurences should be compressed.
Example:
compressMultipleOccurrences( "a b c" )
yields
"a b c"
public static boolean isRegularExpression(java.lang.String s)
s
- The string.
A string is assumed to be a regular expression if it contains any of the followint characters:
* + - [ ] . ^ & \ $ ? { } ? =
public static java.lang.String truncate(java.lang.String str, int n)
str
- String.n
- Number of characters to retain.
public static java.lang.String upperCaseFirstChar(java.lang.String str)
str
- String.
public static java.lang.String removeEnclosingBrackets(java.lang.String str)
str
- String from which to remove enclosing brackets [].
Only one level of brackets is remove. Brackets must be paired to be removed.
Examples:
public static java.lang.String escapeSpecialCharacters(java.lang.String s)
s
- String with possible special chracters to escape.
The following characters are replaced by their "\"-escaped versions: double quote ", single quote ', new line \n, carriage return \r, tab \t, and backslash \.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |