|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.samskivert.util.StringUtil
public class StringUtil
String related utility functions.
| Nested Class Summary | |
|---|---|
static interface |
StringUtil.CharacterValidator
Validates a character. |
static class |
StringUtil.Formatter
Used to format objects in listToString(Object,StringUtil.Formatter). |
| Field Summary | |
|---|---|
protected static NumberFormat |
_ffmt
Used to easily format floats with sensible defaults. |
protected static IntMap<Integer> |
_letterToBits
Maps the 16 most frequent letters in the English language to a number between 0 and 15. |
protected static String |
LINE_SEPARATOR
The line separator for this platform. |
protected static String |
XLATE
Used by hexlate(byte[], int) and unhexlate(java.lang.String). |
| Constructor Summary | |
|---|---|
StringUtil()
|
|
| Method Summary | |
|---|---|
static boolean |
blank(String value)
Deprecated. use isBlank instead. |
static String |
capitalize(String s)
Returns a version of the supplied string with the first letter capitalized. |
static String |
coordsToString(int x,
int y)
Formats a pair of coordinates such that positive values are rendered with a plus prefix and negative values with a minus prefix. |
static void |
coordsToString(StringBuilder buf,
int x,
int y)
Formats a pair of coordinates such that positive values are rendered with a plus prefix and negative values with a minus prefix. |
static String |
decode(String s)
URL decodes the specified string using the UTF-8 character encoding. |
static String |
deNull(String value)
|
protected static String |
digest(String codec,
String source)
Helper function for md5hex(java.lang.String) and sha1hex(java.lang.String). |
static String |
encode(String s)
URL encodes the specified string using the UTF-8 character encoding. |
static String |
fieldsToString(Object object)
Generates a string representation of the supplied object by calling toString(java.lang.Object) on the
contents of its public fields and prefixing that by the name of the fields. |
static String |
fieldsToString(Object object,
String sep)
Like fieldsToString(Object) except that the supplied separator string will be used
between fields. |
static void |
fieldsToString(StringBuilder buf,
Object object)
Appends to the supplied string buffer a representation of the supplied object by calling toString(java.lang.Object) on the contents of its public fields and prefixing that by the name of the
fields. |
static void |
fieldsToString(StringBuilder buf,
Object object,
String sep)
Like fieldsToString(StringBuilder,Object) except that the supplied separator will
be used between fields. |
static String |
fill(char c,
int count)
Returns a string containing the specified character repeated the specified number of times. |
static String |
format(double value)
Formats a floating point value with useful default rules; ie. always display a digit to the left of the decimal and display only two digits to the right of the decimal (rounding as necessary). |
static String |
format(float value)
Formats a floating point value with useful default rules; ie. always display a digit to the left of the decimal and display only two digits to the right of the decimal (rounding as necessary). |
static String |
hexlate(byte[] bytes)
Generates a string from the supplied bytes that is the HEX encoded representation of those bytes. |
static String |
hexlate(byte[] bytes,
int count)
Generates a string from the supplied bytes that is the HEX encoded representation of those bytes. |
static String |
intervalToString(long millis)
Used to convert a time interval to a more easily human readable string of the form: 1d
15h 4m 15s 987m. |
static boolean |
isBlank(String value)
|
static boolean |
isInteger(String value)
Returns whether the supplied string represents an integer value by attempting to parse it with Integer.parseInt(java.lang.String, int). |
static String |
join(Object[] values)
Joins an array of strings (or objects which will be converted to strings) into a single string separated by commas. |
static String |
join(Object[] values,
boolean escape)
Joins an array of strings into a single string, separated by commas, and optionally escaping commas that occur in the individual string values such that a subsequent call to parseStringArray(java.lang.String) would recreate the string array properly. |
static String |
join(Object[] values,
String separator)
Joins the supplied array of strings into a single string separated by the supplied separator. |
protected static String |
join(Object[] values,
String separator,
boolean escape)
Helper function for the various join methods. |
static String |
joinEscaped(String[] values)
Joins an array of strings into a single string, separated by commas, and escaping commas that occur in the individual string values such that a subsequent call to parseStringArray(java.lang.String) would recreate the string array properly. |
static String |
listToString(Object val,
StringUtil.Formatter formatter)
Formats a collection of elements (either an array of objects, an Iterator, an Enumeration or a Collection) using the supplied formatter on each element. |
static void |
listToString(StringBuilder buf,
Object val,
StringUtil.Formatter formatter)
Formats the supplied collection into the supplied string buffer using the supplied formatter. |
static String |
md5hex(String source)
Returns a hex string representing the MD5 encoded source. |
static String |
pad(String value,
int width)
Pads the supplied string to the requested string width by appending spaces to the end of the returned string. |
static boolean[] |
parseBooleanArray(String source)
Parses an array of booleans from its string representation. |
static byte[] |
parseByteArray(String source)
Parses an array of signed byte-sized integers from their string representation. |
static double[] |
parseDoubleArray(String source)
Parses an array of doubles from its string representation. |
static float[] |
parseFloatArray(String source)
Parses an array of floats from it's string representation. |
static int[] |
parseIntArray(String source)
Parses an array of integers from it's string representation. |
static long[] |
parseLongArray(String source)
Parses an array of longs from it's string representation. |
static short[] |
parseShortArray(String source)
Parses an array of short integers from their string representation. |
static String[] |
parseStringArray(String source)
Parses an array of strings from a single string. |
static String[] |
parseStringArray(String source,
boolean intern)
Like parseStringArray(String) but can be instructed to invoke String.intern()
on the strings being parsed into the array. |
static String |
prepad(String value,
int width)
Pads the supplied string to the requested string width by prepending spaces to the end of the returned string. |
static String |
replace(String source,
String before,
String after)
Returns a new string based on source with all instances of before
replaced with after. |
static String |
safeToString(Object object)
Attempts to generate a string representation of the object using Object.toString(),
but catches any exceptions that are thrown and reports them in the returned string
instead. |
static String |
sanitize(String source,
String charRegex)
Sanitize the specified String such that each character must match against the regex specified. |
static String |
sanitize(String source,
StringUtil.CharacterValidator validator)
Sanitize the specified String so that only valid characters are in it. |
static String |
sha1hex(String source)
Returns a hex string representing the SHA-1 encoded source. |
static String |
shortClassName(Class<?> clazz)
Returns the supplied class's name, truncated to one package prior to the actual class name. |
static String |
shortClassName(Object object)
Returns the class name of the supplied object, truncated to one package prior to the actual class name. |
static String |
shortClassName(String name)
Returns the supplied class name truncated to one package prior to the actual class name. |
static String |
spaces(int count)
Returns a string containing the requested number of spaces. |
static String[] |
split(String source,
String sep)
Splits the supplied string into components based on the specified separator string. |
static int |
stringCode(String value)
See stringCode(String,StringBuilder). |
static int |
stringCode(String value,
StringBuilder encoded)
Encodes (case-insensitively) a short English language string into a semi-unique integer. |
static String |
toMatrixString(int[] values,
int colCount,
int fieldWidth)
Returns an array containing the values in the supplied array converted into a table of values wrapped at the specified column count and fit into the specified field width. |
static String |
toString(Object val)
Converts the supplied object to a string. |
static String |
toString(Object val,
String openBox,
String closeBox)
Like the single argument toString(Object) with the additional function of
specifying the characters that are used to box in list and array types. |
static void |
toString(StringBuilder buf,
Object val)
Converts the supplied value to a string and appends it to the supplied string buffer. |
static void |
toString(StringBuilder buf,
Object val,
String openBox,
String closeBox)
Converts the supplied value to a string and appends it to the supplied string buffer. |
static void |
toString(StringBuilder buf,
Object val,
String openBox,
String closeBox,
String sep)
Converts the supplied value to a string and appends it to the supplied string buffer. |
static String |
toUSLowerCase(String s)
Returns a US locale lower case string. |
static String |
toUSUpperCase(String s)
Returns a US locale upper case string. |
static String |
truncate(String s,
int maxLength)
Truncate the specified String if it is longer than maxLength. |
static String |
truncate(String s,
int maxLength,
String append)
Truncate the specified String if it is longer than maxLength. |
static byte[] |
unhexlate(String hex)
Turn a hexlated String back into a byte array. |
static String |
unStudlyName(String name)
Converts a name of the form weAreSoCool to a name of the form
WE_ARE_SO_COOL. |
static String |
wordWrap(String str,
int width)
Wordwraps a string. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static final NumberFormat _ffmt
protected static final String XLATE
hexlate(byte[], int) and unhexlate(java.lang.String).
protected static final IntMap<Integer> _letterToBits
stringCode(java.lang.String).
protected static String LINE_SEPARATOR
| Constructor Detail |
|---|
public StringUtil()
| Method Detail |
|---|
@Deprecated public static boolean blank(String value)
public static boolean isBlank(String value)
public static String deNull(String value)
public static String truncate(String s,
int maxLength)
public static String truncate(String s,
int maxLength,
String append)
append - a String to add to the truncated String only after truncation.public static String capitalize(String s)
public static String toUSLowerCase(String s)
public static String toUSUpperCase(String s)
public static String sanitize(String source,
StringUtil.CharacterValidator validator)
public static String sanitize(String source,
String charRegex)
public static String replace(String source,
String before,
String after)
source with all instances of before
replaced with after.
public static String pad(String value,
int width)
public static String prepad(String value,
int width)
public static String spaces(int count)
public static String fill(char c,
int count)
public static boolean isInteger(String value)
Integer.parseInt(java.lang.String, int).
public static String format(float value)
public static String format(double value)
public static String toString(Object val)
toString() method, but in the case of arrays, toString()
is called on each element and the contents are listed like so:
(value, value, value)Arrays of ints, longs, floats and doubles are also handled for convenience.
Additionally, Enumeration or Iterator objects can be passed
and they will be enumerated and output in a similar manner to arrays. Bear in mind that this
uses up the enumeration or iterator in question.
Also note that passing null will result in the string "null" being returned.
public static String toString(Object val,
String openBox,
String closeBox)
toString(Object) with the additional function of
specifying the characters that are used to box in list and array types. For example, if "["
and "]" were supplied, an int array might be formatted like so: [1, 3, 5].
public static void toString(StringBuilder buf,
Object val)
buf - the string buffer to which we will append the string.val - the value from which to generate the string.
public static void toString(StringBuilder buf,
Object val,
String openBox,
String closeBox)
[1, 3, 5].
buf - the string buffer to which we will append the string.val - the value from which to generate the string.openBox - the opening box character.closeBox - the closing box character.
public static void toString(StringBuilder buf,
Object val,
String openBox,
String closeBox,
String sep)
[1, 3, 5].
buf - the string buffer to which we will append the string.val - the value from which to generate the string.openBox - the opening box character.closeBox - the closing box character.sep - the separator string.
public static String listToString(Object val,
StringUtil.Formatter formatter)
Iterator, an Enumeration or a Collection) using the supplied formatter on each element. Note
that if you simply wish to format a collection of elements by calling Object.toString() on each element, you can just pass the list to the toString(Object) method which will do just that.
public static void listToString(StringBuilder buf,
Object val,
StringUtil.Formatter formatter)
listToString(Object,StringUtil.Formatter) for more details.
public static String fieldsToString(Object object)
toString(java.lang.Object) on the
contents of its public fields and prefixing that by the name of the fields. For example:
[itemId=25, itemName=Elvis, itemCoords=(14, 25)]
public static String fieldsToString(Object object,
String sep)
fieldsToString(Object) except that the supplied separator string will be used
between fields.
public static void fieldsToString(StringBuilder buf,
Object object)
toString(java.lang.Object) on the contents of its public fields and prefixing that by the name of the
fields. For example:
itemId=25, itemName=Elvis, itemCoords=(14, 25)
Note: unlike the version of this method that returns a string, enclosing brackets are not included in the output of this method.
public static void fieldsToString(StringBuilder buf,
Object object,
String sep)
fieldsToString(StringBuilder,Object) except that the supplied separator will
be used between fields.
public static String coordsToString(int x,
int y)
+3+4
-5+7, etc.
public static void coordsToString(StringBuilder buf,
int x,
int y)
+3+4
-5+7, etc.
public static String safeToString(Object object)
Object.toString(),
but catches any exceptions that are thrown and reports them in the returned string
instead. Useful for situations where you can't trust the rat bastards that implemented the
object you're toString()ing.
public static String encode(String s)
public static String decode(String s)
public static String hexlate(byte[] bytes,
int count)
null or empty byte array.
bytes - the bytes for which we want a string representation.count - the number of bytes to stop at (which will be coerced into being <= the length
of the array).public static String hexlate(byte[] bytes)
public static byte[] unhexlate(String hex)
public static String md5hex(String source)
RuntimeException - thrown if the MD5 codec was not available in this JVM.public static String sha1hex(String source)
RuntimeException - thrown if the SHA-1 codec was not available in this JVM.public static byte[] parseByteArray(String source)
25, 17, 21, 99Any inability to parse the short array will result in the function returning null.
public static short[] parseShortArray(String source)
25, 17, 21, 99Any inability to parse the short array will result in the function returning null.
public static int[] parseIntArray(String source)
25, 17, 21, 99Any inability to parse the int array will result in the function returning null.
public static long[] parseLongArray(String source)
25, 17125141422, 21, 99Any inability to parse the long array will result in the function returning null.
public static float[] parseFloatArray(String source)
25.0, .5, 1, 0.99Any inability to parse the array will result in the function returning null.
public static double[] parseDoubleArray(String source)
25.0, .5, 1, 0.99Any inability to parse the array will result in the function returning null.
public static boolean[] parseBooleanArray(String source)
false, false, true, false
public static String[] parseStringArray(String source)
mary, had, a, little, lamb, and, an, escaped, comma,,If a comma is desired in one of the strings, it should be escaped by putting two commas in a row. Any inability to parse the string array will result in the function returning null.
public static String[] parseStringArray(String source,
boolean intern)
parseStringArray(String) but can be instructed to invoke String.intern()
on the strings being parsed into the array.
public static String join(Object[] values)
public static String join(Object[] values,
boolean escape)
parseStringArray(java.lang.String) would recreate the string array properly. Any elements in the values
array that are null will be treated as an empty string.
public static String join(Object[] values,
String separator)
public static String joinEscaped(String[] values)
parseStringArray(java.lang.String) would recreate the string array properly. Any elements in the values
array that are null will be treated as an empty string.
public static String[] split(String source,
String sep)
public static String toMatrixString(int[] values,
int colCount,
int fieldWidth)
toWrappedString(values, 5, 3) might result in output like
so:
12 1 9 10 3 1 5 7 9 11 39 15 12 80 16
public static String intervalToString(long millis)
1d
15h 4m 15s 987m.
public static String shortClassName(Object object)
com.samskivert.util.StringUtil would be reported as
util.StringUtil. If a null object is passed in, null is returned.
public static String shortClassName(Class<?> clazz)
com.samskivert.util.StringUtil would be reported as
util.StringUtil.
public static String shortClassName(String name)
com.samskivert.util.StringUtil would be reported as
util.StringUtil.
public static String unStudlyName(String name)
weAreSoCool to a name of the form
WE_ARE_SO_COOL.
public static int stringCode(String value)
stringCode(String,StringBuilder).
public static int stringCode(String value,
StringBuilder encoded)
This method is useful for mapping a set of string constants to a set of unique integers (e.g. mapping an enumerated type to an integer and back without having to require that the declaration order of the enumerated type remain constant for all time). The caller must, of course, ensure that no collisions occur.
value - the string to be encoded.encoded - if non-null, a string buffer into which the characters used for the encoding
will be recorded.
public static String wordWrap(String str,
int width)
If you want the text to wrap for a graphical display, use a wordwrapping component
such as Label instead.
str - String to word-wrap.width - Maximum line length.
protected static String join(Object[] values,
String separator,
boolean escape)
join methods.
protected static String digest(String codec,
String source)
md5hex(java.lang.String) and sha1hex(java.lang.String).
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||