public final class StringUtil extends Object
Constructor and Description |
---|
StringUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
arrayToString(char[] array)
Converts a character array into a viewable comma delimited
string.
|
static String |
capitalWord(String str)
Converts the specified String to start with a capital letter.
|
static String |
dirName(String str)
Returns the path component of the specified filename.
|
static String |
ellipsis(String str,
int maxlen)
Converts the specified String into a string with maxlen characters, using
... |
static String |
ellipsis(String str,
String ellipsis,
int maxlen)
Converts the specified String into a string with maxlen characters, using the specified
ellipsis as the suffix to denote the missing characters.
|
static String |
escapeDoubleQuotes(String str)
Escapes all double quotes in the specified a string with a backslash
character.
|
static String |
escapeDoubleQuotes(String str,
String escape)
Escapes all double quotes in the specified a string with the specified
escape character.
|
static String |
escapeSingleQuotes(String str)
Escapes all single quotes in the specified a string with a backslash
character.
|
static String |
escapeSingleQuotes(String str,
String escape)
Escapes all single quotes in the specified a string with the specified
escape character.
|
static String |
fileName(String str)
Returns the file component of specified filename.
|
static String |
fixedWidth(String str,
int width)
Converts the specified String into a fixed width string,
left padding (with a blank space) or truncating on the right as necessary.
|
static String |
fixedWidth(String str,
int width,
HAlign align)
Calls
fixedWidth(String, int, HAlign, char) specifying
the padding character as a blank space. |
static String |
fixedWidth(String str,
int width,
HAlign align,
char paddingChar)
Converts the specified String into a fixed width string.
|
static String |
join(List list,
String delim)
Joins the elements of the specified list, delimited by
the specified delimiter.
|
static String |
listToString(List list,
String delim)
Converts a list into a string, each item being seperated by the specified delimiter.
|
static String |
listToString(List list,
String delim,
String start,
String end)
Converts a list into a string, each item being seperated by the specified delimiter.
|
static void |
main(String[] args) |
static boolean |
nullOrEmpty(String str)
Returns true if the specified String is either null or empty.
|
static String |
nullToEmpty(Object val)
Returns an empty string if the specified argument was null,
otherwise returns the value of the toString() method invoked
on the specified object.
|
static String |
nullToEmpty(String val)
Returns an empty string if the specified argument was null,
otherwise returns the argument itself.
|
static String |
remove(String target,
char[] chars)
Removes all occurences of specified characters from the specified
string and returns the new resulting string.
|
static String |
removeBeginningSlash(String str)
Removes the starting (at the very beginning of the string) forward or
backward slash from the specified string (if any) and returns the
resulting String.
|
static String |
removeLeadingWhiteSpace(String target)
Remove any leading whitespace from the target string.
|
static String |
removeSuffix(String name)
Removes the any file extension (.foo for example) from the specified name
and returns the resulting String.
|
static String |
removeTrailingSlash(String str)
Removes the last forward or backward slash from the specified
string (if any) and returns the resulting String.
|
static String |
repeat(char c,
int length)
Returns a String containing a string of the specified character concatenated
the specified number of times.
|
static String |
repeat(String str,
int length)
Returns a String containing the specified string concatenated
the specified number of times.
|
static String |
repeatToWidth(String str,
int length)
Contatenates the given string so that the maximum length reached
is the specified length.
|
static String |
sentenceCase(String str,
String delimiters)
Converts the specified String to be in sentence case, whereby
the first letter of each word in the sentence is uppercased
and all other letters are lowercased.
|
static List |
split(String str,
String delim)
Splits the string using the specified delimiters and
returns the splits parts in a List.
|
static String |
viewableAscii(char c)
A version of
viewableAscii(String) that takes a
single char as a parameter. |
static String |
viewableAscii(String str)
Converts non printable ascii characters in the specified String
to escaped or readable equivalents.
|
public StringUtil()
public static String nullToEmpty(String val)
public static String nullToEmpty(Object val)
public static boolean nullOrEmpty(String str)
public static String repeat(char c, int length)
c
- the character to be repeatedlength
- the repeat lengthpublic static String repeat(String str, int length)
str
- the string to be repeatedlength
- the repeat lengthpublic static String repeatToWidth(String str, int length)
str
- the string to repeatlength
- the length of the returned stringpublic static String fixedWidth(String str, int width)
str
- the target stringwidth
- the fixed widthpublic static String fixedWidth(String str, int width, HAlign align)
fixedWidth(String, int, HAlign, char)
specifying
the padding character as a blank space.public static String fixedWidth(String str, int width, HAlign align, char paddingChar)
str
- the target stringwidth
- the fixed widthalign
- the alignment of the target string within the widthpaddingChar
- the character to pad the string (if necessary);public static String ellipsis(String str, String ellipsis, int maxlen)
str
- the target stringellipsis
- the ellipsis suffixwidth
- the max length, including the ellipsispublic static String ellipsis(String str, int maxlen)
...
as the suffix to denote the missing characters.
str
- the target stringwidth
- the max length, including the ellipsis ("...")public static String removeLeadingWhiteSpace(String target)
target
- the string to remove characters frompublic static String remove(String target, char[] chars)
target
- the string to remove characters fromchars
- an array of characters, each of which is to be removedpublic static String removeTrailingSlash(String str)
public static String removeBeginningSlash(String str)
public static String removeSuffix(String name)
name
- the String denoting the file name to remove the extension frompublic static String dirName(String str)
The functionality of this method is different than java.io.File.getName() and getParent(). Also unix dirname, basename are also compared below. //Using java.io.File (getPath() returns the entire name, identical to //the input, so is not shown. Sample run on windows: Name='' ; getName()=''; getParent()='null' Name='/' ; getName()=''; getParent()='null' Name='/a' ; getName()='a'; getParent()='\' Name='a/b' ; getName()='b'; getParent()='a' Name='a/b.txt' ; getName()='b.txt'; getParent()='a' Name='b.txt' ; getName()='b.txt'; getParent()='null' Name='/a/' ; getName()='a'; getParent()='\' Name='/a/b/' ; getName()='b'; getParent()='\a' Name='a/b/' ; getName()='b'; getParent()='a' ---------------------------- //Using these methods: Name='' ; fileName()=''; dirName()='' Name='/' ; fileName()=''; dirName()='/' Name='/a' ; fileName()='a'; dirName()='/' Name='a/b' ; fileName()='b'; dirName()='a/' Name='a/b.txt' ; fileName()='b.txt'; dirName()='a/' Name='b.txt' ; fileName()='b.txt'; dirName()='' Name='/a/' ; fileName()=''; dirName()='/a/' Name='/a/b/' ; fileName()=''; dirName()='/a/b/' Name='a/b/' ; fileName()=''; dirName()='a/b/' ----------------------------- //unix basename, dirname Name='' ; basename()=''; dirname()='' Name='/' ; basename()='/'; dirname()='/' Name='/a' ; basename()='a'; dirname()='/' Name='a/b' ; basename()='b'; dirname()='a/' Name='a/b.txt' ; basename()='b.txt'; dirname()='a/' Name='b.txt' ; basename()='b.txt'; dirname()='.' Name='/a/' ; basename()='a'; dirname()='/' Name='a/b/' ; basename()='b'; dirname()='a' Name='/a/b/' ; fileName()='b'; dirName()='a' -----------------------------Note, the main differences among the 3 approaches above are in the last 2 statements in each section.
public static String fileName(String str)
public static List split(String str, String delim)
String.split(java.lang.String, int)
instead for greater options.str
- the string to be tokenizeddelim
- delimiter string, each character in the string will be used
as a delimiter to use while tokenizingpublic static String join(List list, String delim)
list
- containing the elements to be joined.delim
- delimits each element from the nextpublic static String capitalWord(String str)
public static String sentenceCase(String str, String delimiters)
public static String viewableAscii(String str)
If the specified String is null, this method returns null.
str
- the String to convertpublic static String viewableAscii(char c)
viewableAscii(String)
that takes a
single char as a parameter.char
- the char to convertpublic static String arrayToString(char[] array)
public static String listToString(List list, String delim)
toString
on that item
so the specified delimiter only applies to the outermost level.
The converted string does not start or end with any characters. To specify the start/end, use listToString(List, String, String, String)
public static String listToString(List list, String delim, String start, String end)
toString
on that item
so the specified delimiter only applies to the outermost level.
The converted string start and ends with the specified chars as well.
public static String escapeSingleQuotes(String str)
public static String escapeSingleQuotes(String str, String escape)
public static String escapeDoubleQuotes(String str)
public static String escapeDoubleQuotes(String str, String escape)