public class StringUtils
extends java.lang.Object
This static class provides various utility methods for manipulating strings.
Modifier and Type | Field and Description |
---|---|
static java.lang.String[] |
asciiControlCharacterNames
Ascii control character character names for display.
|
Modifier | Constructor and Description |
---|---|
protected |
StringUtils()
Don't allow instantiation, do allow overrides.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
areOrAreNot(boolean bool)
Return "are", "are not" based upon boolean value.
|
static java.lang.String |
arrayToString(java.lang.Object[] array)
Convert array contents to string.
|
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 |
countChar(java.lang.String s,
char ch)
Count occurrences of character in string.
|
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 |
displayableString(java.lang.String s)
Displayable string with control characters mapped to
|
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 java.lang.String |
endOfString(java.lang.String str,
int n)
Extract characters from end of a 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 |
getStringFromUTF8Bytes(byte[] bytes)
Get string from utf-8 bytes.
|
static byte[] |
getUTF8Bytes(java.lang.String s)
Get utf-8 bytes for a string.
|
static java.lang.String |
haveOrHaveNot(boolean bool)
Return "have", "have not" based upon boolean value.
|
static int |
indexOfIgnoreCase(java.lang.String str1,
java.lang.String str2)
Searches a string for a case-insensitive substring match.
|
static int |
indexOfIgnoreCase(java.lang.String str1,
java.lang.String str2,
int fromIndex)
Searches a string for a case-insensitive substring match.
|
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 |
join(java.lang.Iterable<? extends java.lang.CharSequence> sequence)
Join members of a character sequence into a string.
|
static java.lang.String |
join(java.lang.Iterable<? extends java.lang.CharSequence> sequence,
java.lang.String delimeter)
Join members of a character sequence into a string.
|
static java.lang.String |
join(java.lang.Object[] array,
java.lang.String delimeter)
Join members of an array into a string.
|
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 int |
matchingFinalCharacters(java.lang.String s1,
java.lang.String s2)
Compute length of matching final strings.
|
static int |
matchingInitialCharacters(java.lang.String s1,
java.lang.String s2)
Compute length of matching initial strings.
|
static java.lang.String |
objectToString(java.lang.Object object)
Convert object to string representation.
|
static java.lang.String |
ordinalize(int number,
boolean addCommas)
Convert number to ordinal English form.
|
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 |
pluralize(long count,
java.lang.String singular,
java.lang.String plural)
Select singular or plural string based upon count.
|
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 |
replaceFirst(java.lang.String str,
java.lang.String s,
java.lang.String r)
Replaces first substring of a string.
|
static java.lang.String |
reverseString(java.lang.String s)
Reverse 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 |
startOfString(java.lang.String str,
int n)
Extract characters from start of a string..
|
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 |
stripCharacterRuns(java.lang.String s)
Remove runs of multiple characters from a string.
|
static java.lang.String |
stripChars(java.lang.String s,
java.lang.String charsToStrip)
Strip specified characters from a string.
|
static java.lang.String |
substring(java.lang.String str,
int start,
int end)
Find substring of a string.
|
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.
|
public static final java.lang.String[] asciiControlCharacterNames
protected StringUtils()
public static java.lang.String arrayToString(java.lang.Object[] array)
array
- The array to convert to a string.N.B. This method assumes the array values have toString() methods.
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 countChar(java.lang.String s, char ch)
s
- The string.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 displayableString(java.lang.String s)
s
- The string.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 byte[] getUTF8Bytes(java.lang.String s)
s
- The string.public static java.lang.String getStringFromUTF8Bytes(byte[] bytes)
bytes
- Byte array to convert to string.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 indexOfIgnoreCase(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 at which to start the search.public static int indexOfIgnoreCase(java.lang.String str1, java.lang.String str2)
str1
- The string to be searched.str2
- The substring to search for, converted to lower case.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)
str
- The string.s
- Substring to be replaced.r
- Replacement string.public static java.lang.String replaceFirst(java.lang.String str, java.lang.String s, java.lang.String r)
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 pluralize(long 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 startOfString(java.lang.String str, int n)
str
- The string.n
- Maximum number of characters to extract.public static java.lang.String endOfString(java.lang.String str, int n)
str
- The string.n
- Maximum number of characters to extract.public static java.lang.String substring(java.lang.String str, int start, int end)
str
- The string.start
- Starting character position (0 origin) of substringend
- Ending character + 1 position of substring.Works like usual String class substring, but handles problem values for start/end values without throwing exceptions.
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 stripChars(java.lang.String s, java.lang.String charsToStrip)
s
- The string containing characters to strip.charsToStrip
- The characters to strip.public static java.lang.String stripCharacterRuns(java.lang.String s)
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 int matchingInitialCharacters(java.lang.String s1, java.lang.String s2)
s1
- First string.s2
- Second string.public static int matchingFinalCharacters(java.lang.String s1, java.lang.String s2)
s1
- First string.s2
- Second string.public static java.lang.String reverseString(java.lang.String s)
s
- String to reverse.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 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 \.
public static java.lang.String join(java.lang.Iterable<? extends java.lang.CharSequence> sequence, java.lang.String delimeter)
sequence
- Character sequence to join into a string.delimeter
- String to separate elements of sequence.public static java.lang.String join(java.lang.Object[] array, java.lang.String delimeter)
array
- Array to join into a string.delimeter
- String to separate elements of sequence.public static java.lang.String join(java.lang.Iterable<? extends java.lang.CharSequence> sequence)
sequence
- Character sequence to join into a string.public static java.lang.String ordinalize(int number, boolean addCommas)
number
- Number for which to get the ordinal value.addCommas
- True to format numeric portion with commas.