public final class StringUtil extends java.lang.Object
Constructor and Description |
---|
StringUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
arrayToString(char[] array)
Converts a character array into a viewable comma delimited
string.
|
static java.lang.String |
capitalWord(java.lang.String str)
Converts the specified String to start with a capital letter.
|
static java.lang.String |
dirName(java.lang.String str)
Returns the path component of the specified filename.
|
static java.lang.String |
ellipsis(java.lang.String str,
int maxlen)
Converts the specified String into a string with maxlen characters, using
... |
static java.lang.String |
ellipsis(java.lang.String str,
java.lang.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 java.lang.String |
escapeDoubleQuotes(java.lang.String str)
Escapes all double quotes in the specified a string with a backslash
character.
|
static java.lang.String |
escapeDoubleQuotes(java.lang.String str,
java.lang.String escape)
Escapes all double quotes in the specified a string with the specified
escape character.
|
static java.lang.String |
escapeSingleQuotes(java.lang.String str)
Escapes all single quotes in the specified a string with a backslash
character.
|
static java.lang.String |
escapeSingleQuotes(java.lang.String str,
java.lang.String escape)
Escapes all single quotes in the specified a string with the specified
escape character.
|
static java.lang.String |
fileName(java.lang.String str)
Returns the file component of specified filename.
|
static java.lang.String |
fixedWidth(java.lang.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 java.lang.String |
fixedWidth(java.lang.String str,
int width,
HAlign align)
Calls
fixedWidth(String, int, HAlign, char) specifying
the padding character as a blank space. |
static java.lang.String |
fixedWidth(java.lang.String str,
int width,
HAlign align,
char paddingChar)
Converts the specified String into a fixed width string.
|
static java.lang.String |
join(java.util.List list,
java.lang.String delim)
Joins the elements of the specified list, delimited by
the specified delimiter.
|
static java.lang.String |
listToString(java.util.List list,
java.lang.String delim)
Converts a list into a string, each item being seperated by the specified delimiter.
|
static java.lang.String |
listToString(java.util.List list,
java.lang.String delim,
java.lang.String start,
java.lang.String end)
Converts a list into a string, each item being seperated by the specified delimiter.
|
static void |
main(java.lang.String[] args) |
static boolean |
nullOrEmpty(java.lang.String str)
Returns true if the specified String is either null or empty.
|
static java.lang.String |
nullToEmpty(java.lang.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 java.lang.String |
nullToEmpty(java.lang.String val)
Returns an empty string if the specified argument was null,
otherwise returns the argument itself.
|
static java.lang.String |
remove(java.lang.String target,
char[] chars)
Removes all occurences of specified characters from the specified
string and returns the new resulting string.
|
static java.lang.String |
removeBeginningSlash(java.lang.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 java.lang.String |
removeSuffix(java.lang.String name)
Removes the any file extension (.foo for example) from the specified name
and returns the resulting String.
|
static java.lang.String |
removeTrailingSlash(java.lang.String str)
Removes the last forward or backward slash from the specified
string (if any) and returns the resulting String.
|
static java.lang.String |
repeat(char c,
int length)
Returns a String containing a string of the specified character concatenated
the specified number of times.
|
static java.lang.String |
repeat(java.lang.String str,
int length)
Returns a String containing the specified string concatenated
the specified number of times.
|
static java.lang.String |
repeatToWidth(java.lang.String str,
int length)
Contatenates the given string so that the maximum length reached
is the specified length.
|
static java.lang.String |
sentenceCase(java.lang.String str,
java.lang.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 java.util.List |
split(java.lang.String str,
java.lang.String delim)
Splits the string using the specified delimiters and
returns the splits parts in a List.
|
static java.lang.String |
viewableAscii(char c)
A version of
viewableAscii(String) that takes a
single char as a parameter. |
static java.lang.String |
viewableAscii(java.lang.String str)
Converts non printable ascii characters in the specified String
to escaped or readable equivalents.
|
public StringUtil()
public static java.lang.String nullToEmpty(java.lang.String val)
public static java.lang.String nullToEmpty(java.lang.Object val)
public static boolean nullOrEmpty(java.lang.String str)
public static java.lang.String repeat(char c, int length)
c
- the character to be repeatedlength
- the repeat lengthpublic static java.lang.String repeat(java.lang.String str, int length)
str
- the string to be repeatedlength
- the repeat lengthpublic static java.lang.String repeatToWidth(java.lang.String str, int length)
str
- the string to repeatlength
- the length of the returned stringpublic static java.lang.String fixedWidth(java.lang.String str, int width)
str
- the target stringwidth
- the fixed widthpublic static java.lang.String fixedWidth(java.lang.String str, int width, HAlign align)
fixedWidth(String, int, HAlign, char)
specifying
the padding character as a blank space.public static java.lang.String fixedWidth(java.lang.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 java.lang.String ellipsis(java.lang.String str, java.lang.String ellipsis, int maxlen)
str
- the target stringellipsis
- the ellipsis suffixwidth
- the max length, including the ellipsispublic static java.lang.String ellipsis(java.lang.String str, int maxlen)
...
as the suffix to denote the missing characters.
str
- the target stringwidth
- the max length, including the ellipsis ("...")public static java.lang.String remove(java.lang.String target, char[] chars)
target
- the string to remove characters fromchars
- an array of characters, each of which is to be removedpublic static java.lang.String removeTrailingSlash(java.lang.String str)
public static java.lang.String removeBeginningSlash(java.lang.String str)
public static java.lang.String removeSuffix(java.lang.String name)
name
- the String denoting the file name to remove the extension frompublic static java.lang.String dirName(java.lang.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 java.lang.String fileName(java.lang.String str)
public static java.util.List split(java.lang.String str, java.lang.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 java.lang.String join(java.util.List list, java.lang.String delim)
list
- containing the elements to be joined.delim
- delimits each element from the nextpublic static java.lang.String capitalWord(java.lang.String str)
public static java.lang.String sentenceCase(java.lang.String str, java.lang.String delimiters)
public static java.lang.String viewableAscii(java.lang.String str)
If the specified String is null, this method returns null.
str
- the String to convertpublic static java.lang.String viewableAscii(char c)
viewableAscii(String)
that takes a
single char as a parameter.char
- the char to convertpublic static java.lang.String arrayToString(char[] array)
public static java.lang.String listToString(java.util.List list, java.lang.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 java.lang.String listToString(java.util.List list, java.lang.String delim, java.lang.String start, java.lang.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 java.lang.String escapeSingleQuotes(java.lang.String str)
public static java.lang.String escapeSingleQuotes(java.lang.String str, java.lang.String escape)
public static java.lang.String escapeDoubleQuotes(java.lang.String str)
public static java.lang.String escapeDoubleQuotes(java.lang.String str, java.lang.String escape)
public static void main(java.lang.String[] args)