|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.aliasi.util.Strings
public class Strings
Static utility methods for processing strings, characters and string buffers.
Character,
String,
StringBuilder| Field Summary | |
|---|---|
static String |
ASCII
String representing the ASCII encoding for characters. |
static char |
DEFAULT_SEPARATOR_CHAR
The default separator character, a single space. |
static String |
DEFAULT_SEPARATOR_STRING
The default separator string. |
static char[] |
EMPTY_CHAR_ARRAY
The zero-length character array. |
static String |
EMPTY_STRING
The empty string. |
static String[][] |
EMPTY_STRING_2D_ARRAY
The zero-length two-dimensional array of strings. |
static String[] |
EMPTY_STRING_ARRAY
The zero-length string array. |
static String |
Latin1
String representing the Latin1 encoding for characters. |
static char |
NBSP_CHAR
The non-breakable space character. |
static char |
NEWLINE_CHAR
The newline character. |
static String |
SINGLE_SPACE_STRING
A string consisting of a single space. |
static String |
UTF8
String representing the UTF-8 encoding for characters. |
| Method Summary | |
|---|---|
static boolean |
allDigits(char[] cs,
int start,
int length)
Returns true if all of the characters
in the specified range are digits. |
static boolean |
allDigits(String s)
Returns true if all of the characters
making up the specified string are digits. |
static boolean |
allLetters(char[] chars)
Returns true if all of the characters in the
specified array are letters. |
static boolean |
allLowerCase(char[] chars)
Returns true if all of the characters in the
specified array are lower case letters. |
static boolean |
allLowerCase(CharSequence token)
Returns true if the specified character sequence
contains only lowercase letters. |
static boolean |
allPunctuation(char[] chars)
Returns true if all of the characters in the
specified array are punctuation as specified by
isPunctuation(char). |
static boolean |
allPunctuation(String token)
Returns true if all of the characters in the
specified string are punctuation as specified by
isPunctuation(char). |
static boolean |
allSymbols(char[] cs)
Returns true if none of the characters in the
specified array are letters or digits. |
static boolean |
allUpperCase(char[] chars)
Returns true if all of the characters in the
specified array are upper case letters. |
static boolean |
allWhitespace(char[] ch,
int start,
int length)
Returns true if the specified range of the
specified character array only whitespace characters, as defined for
characters by isWhitespace(char c). |
static boolean |
allWhitespace(String s)
Returns true if the specified string contains
only whitespace characters. |
static boolean |
allWhitespace(StringBuilder sb)
Returns true if the specified buffer contains
only whitespace characters. |
static String |
bytesToHex(byte[] bytes)
Returns a hexadecimal string-based representation of the specified byte array. |
static String |
byteToHex(byte b)
Deprecated. Use Integer.toHexString(b) instead. |
static boolean |
capitalized(char[] chars)
Returns true if the first character in the
specified array is an upper case letter and all subsequent
characters are lower case letters. |
static void |
checkArgsStartEnd(char[] cs,
int start,
int end)
Throws an exception if the start and end plus one indices are not in the range for the specified array of characters. |
static String |
concatenate(Object[] xs)
Concatenate the elements of the specified array as strings, separating with the default separator DEFAULT_SEPARATOR_STRING. |
static String |
concatenate(Object[] xs,
int start,
int end)
Concatenate the elements of the specified array as strings, starting at the object at the specified index and continuing through one element before the specified end index, separating with the default spacer DEFAULT_SEPARATOR_STRING. |
static String |
concatenate(Object[] xs,
int start,
int end,
String spacer)
Concatenate the elements of the specified array as strings, starting at the object at the specified index and continuing through one element before the specified end index, separating with the specified spacer. |
static String |
concatenate(Object[] xs,
int start,
String spacer)
Concatenate the elements of the specified array as strings, starting at the object at the specified index and continuing through the rest of the string, separating with the specified string spacer. |
static String |
concatenate(Object[] xs,
String spacer)
Concatenate the elements of the specified array as strings, separating with the specified string spacer. |
static boolean |
containsChar(String s,
char c)
Returns true if the specified string contains
an instance of the specified character. |
static boolean |
containsDigits(char[] chars)
Returns true if at least one of the characters in
the specified array is a digit. |
static boolean |
containsLetter(char[] chars)
Returns true if at least one of the characters in
the specified array is a letter. |
static char |
deAccentLatin1(char c)
Returns the equivalent de-accented character for characters in the Latin-1 (ISO-8859-1) range (0000-00FF). |
static String |
deAccentLatin1(CharSequence cSeq)
Returns the string constructed from the specified character sequence by deaccenting each of its characters. |
static String |
decimalFormat(double x,
String pattern,
int length)
Deprecated. Use DecimalFormat directly with a pattern, or use the
newer Formatter class or String.format(String,Object[]) utility
method instead. |
static boolean |
equalCharSequence(CharSequence cs1,
CharSequence cs2)
Return true if the two character sequences have
the same length and the same characters. |
static String |
fit(String in,
int length)
Deprecated. Use java.util.Formatter instead. |
static String |
functionArgs(String functionName,
Object[] args)
Return a string representation of a function applied to its arguments. |
static String |
functionArgsList(Object[] args)
Returns a string representation of the specified array as a function's argument list. |
static int |
hashCode(CharSequence cSeq)
Returns a hash code for a character sequence that is equivalent to the hash code generated for a its string yield. |
static void |
indent(StringBuilder sb,
int length)
Appends an ``indentation'' to the specified string buffer, consisting of a newline character and the specified number of space characters to the specified string buffer. |
static boolean |
isPunctuation(char c)
Returns true if specified character is a punctuation character. |
static boolean |
isWhitespace(char c)
Returns true if specified character is a whitespace character. |
static String |
msToString(long ms)
Takes a time in milliseconds and returns an hours, minutes and seconds representation. |
static String |
normalizeWhitespace(CharSequence cs)
Returns a whitespace-normalized version of the specified character sequence. |
static void |
normalizeWhitespace(CharSequence cs,
StringBuilder sb)
Appends a whitespace-normalized form of the specified character sequence into the specified string buffer. |
static String |
nsToString(long ns)
Takes a time in nanoseconds and returns an hours, minutes and seconds representation. |
static String |
padding(int length)
Returns a string consisting of the specified number of default separator characters DEFAULT_SEPARATOR_CHAR. |
static void |
padding(StringBuilder sb,
int length)
Append the specified number of default separator characters DEFAULT_SEPARATOR_CHAR to the specified string buffer. |
static String |
power(String s,
int count)
Returns the result of concatenating the specified number of copies of the specified string. |
static String[] |
readArrayFrom(DataInput dataIn)
Deprecated. Build this yourself using DataInput.readUTF(). |
static String |
reverse(CharSequence cs)
Return the string that is the reverse of the specified character sequence. |
static String |
save(String s)
Deprecated. Use new String(s) instead. |
static int |
sharedPrefixLength(String a,
String b)
Returns the length of the longest shared prefix of the two input strings. |
static String[] |
split(String s,
char c)
Returns an array of substrings of the specified string, in order, with divisions before and after any instance of the specified character. |
static String |
titleCase(String word)
Returns a title-cased version of the specified word, which involves capitalizing the first character in the word if it is a letter. |
static char[] |
toCharArray(CharSequence cSeq)
Returns an array of characters corresponding to the specified character sequence. |
static void |
writeArrayTo(DataOutput dataOut,
String[] strings)
Deprecated. Write your own with DataOutput.writeUTF(String) instead. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static String UTF8
public static String Latin1
public static String ASCII
public static char NBSP_CHAR
public static char NEWLINE_CHAR
public static char DEFAULT_SEPARATOR_CHAR
public static String DEFAULT_SEPARATOR_STRING
1, consisting of the default separator character
DEFAULT_SEPARATOR_CHAR.
public static final String SINGLE_SPACE_STRING
public static final String EMPTY_STRING
public static final char[] EMPTY_CHAR_ARRAY
public static final String[] EMPTY_STRING_ARRAY
public static final String[][] EMPTY_STRING_2D_ARRAY
| Method Detail |
|---|
@Deprecated public static String save(String s)
new String(s) instead.
s - String to copy.
public static String reverse(CharSequence cs)
public static boolean containsChar(String s,
char c)
true if the specified string contains
an instance of the specified character.
s - String to check for character.c - Character.
true if specified character occurs in
specified string.public static boolean allWhitespace(StringBuilder sb)
true if the specified buffer contains
only whitespace characters.
sb - String buffer to test for whitespace.
true if the specified buffer contains only
whitespace characters.public static boolean allWhitespace(String s)
true if the specified string contains
only whitespace characters.
s - Stirng to test for whitespace.
true if the specified string contains only
whitespace characters.
public static boolean allWhitespace(char[] ch,
int start,
int length)
true if the specified range of the
specified character array only whitespace characters, as defined for
characters by isWhitespace(char c).
ch - Character array to test for whitespace characters in range.start - Beginning of range to test.length - Number of characters to test.
true if the specified string contains only
whitespace characters.public static boolean isWhitespace(char c)
Character.isWhitespace(char) is extended to include
the unicode non-breakable space character (unicode 160).
c - Character to test.
true if specified character is a
whitespace.Character.isWhitespace(char)
public static void normalizeWhitespace(CharSequence cs,
StringBuilder sb)
" a\tb\n"
would append the following characters to "a
b".
This command is useful for text inputs for web or GUI applications.
cs - Character sequence whose normalization is appended to
the buffer.sb - String buffer to which the normalized character
sequence is appended.public static String normalizeWhitespace(CharSequence cs)
normalizeWhitespace(CharSequence,StringBuilder) for
information on the normalization procedure.
cs - Character sequence to normalize.
public static boolean allDigits(String s)
true if all of the characters
making up the specified string are digits.
s - String to test.
true if all of the characters making up
the specified string are digits.
public static boolean allDigits(char[] cs,
int start,
int length)
true if all of the characters
in the specified range are digits.
cs - Underlying characters to test.start - Index of first character to test.length - Number of characters to test.
true if all of the characters making up
the specified string are digits.public static boolean isPunctuation(char c)
c - Character to test.
true if specified character is a
whitespace.Character
public static String power(String s,
int count)
s - String to concatenate.count - Number of copies of string to concatenate.
public static String concatenate(Object[] xs)
DEFAULT_SEPARATOR_STRING.
xs - Array of objects whose string representations are
concatenated.
public static String concatenate(Object[] xs,
String spacer)
xs - Array of objects whose string representations are
concatenated.spacer - String to insert between the string
representations.
public static String concatenate(Object[] xs,
int start,
String spacer)
xs - Array of objects whose string representations are
concatenated.start - Index of first object to include.spacer - String to insert between the string
v * representations.
public static String concatenate(Object[] xs,
int start,
int end)
DEFAULT_SEPARATOR_STRING.
xs - Array of objects whose string representations are
concatenated.start - Index of first object to include.end - The index of the last element to include plus
1.
public static String concatenate(Object[] xs,
int start,
int end,
String spacer)
xs - Array of objects whose string representations are
concatenated.start - Index of first object to include.end - The index of the last element to include plus
1.spacer - String to insert between the string
representations.
public static void indent(StringBuilder sb,
int length)
sb - String buffer to indent.length - Number of spaces to append after a newline to the
specified string buffer.
@Deprecated
public static String fit(String in,
int length)
java.util.Formatter instead.
in - String to fit to specified length.length - Length to fit.
public static String padding(int length)
DEFAULT_SEPARATOR_CHAR.
length - Number of separator characters in returned
string.
public static void padding(StringBuilder sb,
int length)
DEFAULT_SEPARATOR_CHAR to the specified string buffer.
sb - String buffer to which to append specified number of
default separator characters.length - Number of separator characters to append.
public static String functionArgs(String functionName,
Object[] args)
functionName - Name of function.args - Arguments to function.
public static String functionArgsList(Object[] args)
args - Objects to represent arguments.
public static boolean allLowerCase(char[] chars)
true if all of the characters in the
specified array are lower case letters.
chars - Array of characters to test.
true if all of the characters in the
specified array are lower case letters.public static boolean allLowerCase(CharSequence token)
true if the specified character sequence
contains only lowercase letters. The test is performed by
Character.isLowerCase(char). This is the same test as
performed by allLowerCase(char[]).
token - Token to check.
true if token is all lower-case.public static boolean allUpperCase(char[] chars)
true if all of the characters in the
specified array are upper case letters.
chars - Array of characters to test.
true if all of the characters in the
specified array are upper case letters.public static boolean allLetters(char[] chars)
true if all of the characters in the
specified array are letters.
chars - Array of characters to test.
true if all of the characters in the
specified array are letters.public static boolean allPunctuation(char[] chars)
true if all of the characters in the
specified array are punctuation as specified by
isPunctuation(char).
chars - Array of characters to test.
true if all of the characters in the
specified array are punctuation.public static boolean allPunctuation(String token)
true if all of the characters in the
specified string are punctuation as specified by
isPunctuation(char).
token - Token string to test.
true if all of the characters in the
specified string are punctuation.
public static String[] split(String s,
char c)
| Call | Result |
split("",' ') |
{ "" } |
split("a",' ') |
{ "a" } |
split("a b",' ') |
{ "a", "b" } |
split("aaa bb cccc",' ') |
{ "aaa", "bb", "cccc" } |
split(" a",' ') |
{ "", "a" } |
split("a ",' ') |
{ "a", "" } |
split(" a ",' ') |
{ "", "a", "" } |
s - String to split.c - Character on which to split the string.
public static boolean allSymbols(char[] cs)
true if none of the characters in the
specified array are letters or digits.
cs - Array of characters to test.
true if none of the characters in the
specified array are letters or digits.public static boolean containsDigits(char[] chars)
true if at least one of the characters in
the specified array is a digit.
chars - Array of characters to test.
true if at least one of the characters in
the specified array is a digit.public static boolean containsLetter(char[] chars)
true if at least one of the characters in
the specified array is a letter.
chars - Array of characters to test.
true if at least one of the characters in
the specified array is a letter.public static boolean capitalized(char[] chars)
true if the first character in the
specified array is an upper case letter and all subsequent
characters are lower case letters.
chars - Array of characters to test.
true if all of the characters in the
specified array are lower case letters.public static String titleCase(String word)
word - The word to convert to title case.
public static String bytesToHex(byte[] bytes)
byteToHex(byte) and the results are concatenated into
the final string representation. Letter-based digits are
lowercase.
bytes - Array of bytes to convert.
@Deprecated public static String byteToHex(byte b)
Integer.toHexString(b) instead.
Math.byteAsUnsigned(byte). The result will always be two
characters, even if the unsigned byte value is less than 16.
Letter-based digits are lowercase.
b - Byte to convert.
@Deprecated
public static void writeArrayTo(DataOutput dataOut,
String[] strings)
throws IOException
DataOutput.writeUTF(String) instead.
DataOutput.writeUTF(String).
dataOut - Data output stream to which to write the string.strings - Array of strings to write.
IOException - If there is an I/O exception writing to the
stream.
@Deprecated
public static String[] readArrayFrom(DataInput dataIn)
throws IOException
DataInput.readUTF().
writeArrayTo(DataOutput,String[]) for
information on the encoding used.
dataIn - Data input stream from which array is read.
IOException - If there is an I/O exception reading from
the stream.
@Deprecated
public static String decimalFormat(double x,
String pattern,
int length)
DecimalFormat directly with a pattern, or use the
newer Formatter class or String.format(String,Object[]) utility
method instead.
For a full description Java's decimal formatting pattern
language, see DecimalFormat.
For English-style number formatting, the following patterns are examples that allow arbitrarily long whole-number portions, and exactly two decimal places. Examples for formatting two numbers are given in the last two columns.
If a variable-length decimal portion is required, the
Pattern Leading Zero? Thousands Commas? 2798.389 0.391 "#,##0.00"yes yes 2,798.39 0.39 "#0.00"yes no 2798.39 0.39 "#,###.00"no yes 2,798.39 .39 "#.00"no no 2798.39 .39
0s may be replaced with #s. Note
that the formatted numbers are rounded, not truncated; see
BigDecimal.ROUND_HALF_EVEN for a full description.
x - The number to format.pattern - The decimal pattern used to guide formatting.length - Length of result in characters.
IllegalArgumentException - If the length is not positive or
the pattern is not well formed.
public static void checkArgsStartEnd(char[] cs,
int start,
int end)
cs - Array of characters.start - Index of first character.end - Index of one past last character.
IndexOutOfBoundsException - If the specified indices are out of
bounds of the specified character array.public static char[] toCharArray(CharSequence cSeq)
cSeq - Character sequence to convert.
public static String nsToString(long ns)
msToString(long) for more
information on output format.
Recall that 1 second = 1,000,000,000 nanoseconds.
ns - Amount of time in nanoseconds.public static String msToString(long ms)
Recall that 1 second = 1000 milliseconds.
| Input ms | Output String |
| 0 | :00 |
| 999 | :00 |
| 1001 | :01 |
| 32,000 | :32 |
| 61,000 | 1:01 |
| 11,523,000 | 3:12:03 |
ms - Time in milliseconds.
public static boolean equalCharSequence(CharSequence cs1,
CharSequence cs2)
true if the two character sequences have
the same length and the same characters. Recall that equality
is not refined in the specification of CharSequence, but
rather inherited from Object.equals(Object).
The related method hashCode(CharSequence) returns
hash codes consistent with this notion of equality.
cs1 - First character sequence.cs2 - Second character sequence.
true if the character sequences yield
the same strings.public static int hashCode(CharSequence cSeq)
CharSequence does not refine the definition
of equality beyond that of Object.equals(Object).
The return result is the same as would be produced by:
hashCode(cSeq) = cSeq.toString().hashCode()
Recall that the CharSequence interface requires its
CharSequence.toString() to return a string
corresponding to its characters as returned by
charAt(0),...,charAt(length()-1). This value
can be defined directly by inspecting the hash code for strings:
int h = 0;
for (int i = 0; i < cSeq.length(); ++i)
h = 31*h + cSeq.charAt(i);
return h;
cSeq - The character sequence.
public static char deAccentLatin1(char c)
The
unicode.org site supplies a complete Latin-1
Supplement, listing the code points for each character.
c - Character to de-accent.
public static String deAccentLatin1(CharSequence cSeq)
deAccentLatin1(char) for details of the de-accenting.
cSeq - Character sequence to de accent.
public static int sharedPrefixLength(String a,
String b)
a - First string.b - Second string.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||