Class StringUtil
java.lang.Object
fc.util.StringUtil
Utility functions related to java.lang.String, i.e
functions that are useful but not provided by that class.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringarrayToString(char[] array) Converts a character array into a viewable comma delimited string.static StringcapitalWord(String str) Converts the specified String to start with a capital letter.static StringReturns the path component of the specified filename.static StringConverts the specified String into a string with maxlen characters, using...as the suffix to denote the missing characters.static StringConverts the specified String into a string with maxlen characters, using the specified ellipsis as the suffix to denote the missing characters.static StringescapeDoubleQuotes(String str) Escapes all double quotes in the specified a string with a backslash character.static StringescapeDoubleQuotes(String str, String escape) Escapes all double quotes in the specified a string with the specified escape character.static StringescapeSingleQuotes(String str) Escapes all single quotes in the specified a string with a backslash character.static StringescapeSingleQuotes(String str, String escape) Escapes all single quotes in the specified a string with the specified escape character.static StringReturns the file component of specified filename.static StringfixedWidth(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 StringfixedWidth(String str, int width, HAlign align) CallsfixedWidth(java.lang.String,int,fc.util.HAlign,char)specifying the padding character as a blank space.static StringfixedWidth(String str, int width, HAlign align, char paddingChar) Converts the specified String into a fixed width string.static StringJoins the elements of the specified list, delimited by the specified delimiter.static StringlistToString(List list, String delim) Converts a list into a string, each item being seperated by the specified delimiter.static StringlistToString(List list, String delim, String start, String end) Converts a list into a string, each item being seperated by the specified delimiter.static voidstatic booleannullOrEmpty(String str) Returns true if the specified String is either null or empty.static StringnullToEmpty(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 StringnullToEmpty(String val) Returns an empty string if the specified argument was null, otherwise returns the argument itself.static StringRemoves all occurences of specified characters from the specified string and returns the new resulting string.static StringRemoves 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 StringremoveLeadingWhiteSpace(String target) Remove any leading whitespace from the target string.static StringremoveSuffix(String name) Removes the any file extension (.foo for example) from the specified name and returns the resulting String.static StringRemoves the last forward or backward slash from the specified string (if any) and returns the resulting String.static Stringrepeat(char c, int length) Returns a String containing a string of the specified character concatenated the specified number of times.static StringReturns a String containing the specified string concatenated the specified number of times.static StringrepeatToWidth(String str, int length) Contatenates the given string so that the maximum length reached is the specified length.static StringsentenceCase(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 ListSplits the string using the specified delimiters and returns the splits parts in a List.static StringviewableAscii(char c) A version ofviewableAscii(java.lang.String)that takes a single char as a parameter.static StringviewableAscii(String str) Converts non printable ascii characters in the specified String to escaped or readable equivalents.
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
nullToEmpty
Returns an empty string if the specified argument was null, otherwise returns the argument itself. -
nullToEmpty
Returns an empty string if the specified argument was null, otherwise returns the value of the toString() method invoked on the specified object. -
nullOrEmpty
Returns true if the specified String is either null or empty. -
repeat
-
repeat
-
repeatToWidth
Contatenates the given string so that the maximum length reached is the specified length. If the specified string does not fit in the length, then the string is truncated appropriately.- Parameters:
str- the string to repeatlength- the length of the returned string
-
fixedWidth
Converts the specified String into a fixed width string, left padding (with a blank space) or truncating on the right as necessary. (i.e., the specified string is aligned left w.r.t to the specified width).- Parameters:
str- the target stringwidth- the fixed width- Returns:
- the transformed string
-
fixedWidth
CallsfixedWidth(java.lang.String,int,fc.util.HAlign,char)specifying the padding character as a blank space. -
fixedWidth
Converts the specified String into a fixed width string. Strings lesser in size than the fixed width are padded or truncated as required by the specified alignment.- Parameters:
str- the target stringwidth- the fixed widthalign- the alignment of the target string within the widthpaddingChar- the character to pad the string (if necessary);- Returns:
- the transformed string
-
ellipsis
Converts the specified String into a string with maxlen characters, using the specified ellipsis as the suffix to denote the missing characters.- Parameters:
str- the target stringellipsis- the ellipsis suffixwidth- the max length, including the ellipsis- Returns:
- the transformed string
-
ellipsis
Converts the specified String into a string with maxlen characters, using...as the suffix to denote the missing characters.- Parameters:
str- the target stringwidth- the max length, including the ellipsis ("...")- Returns:
- the transformed string
-
removeLeadingWhiteSpace
Remove any leading whitespace from the target string. Trailing whitespace is not affected.- Parameters:
target- the string to remove characters from
-
remove
-
removeTrailingSlash
Removes the last forward or backward slash from the specified string (if any) and returns the resulting String. Returns null if a null argument is specified. The trailing slash is a slash that is the last character of the specified string. -
removeBeginningSlash
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. Returns null if null argument is specified. -
removeSuffix
Removes the any file extension (.foo for example) from the specified name and returns the resulting String. Returns null if the specified path was null. This method takes a String as a parameter, as opposed to a java.io.File because the caller knows best what portion of the filename should be modified and returned by this method (full path name, name without path information etc).- Parameters:
name- the String denoting the file name to remove the extension from
-
dirName
Returns the path component of the specified filename. If no path exists or the specified string is null, returns the empty string "". The path separator in the specified string should be /. If on a platform where this is not the case, the specified string should be modified to contain "/" before invoking this method. The returned pathName does contain the trailing "/". This ensures that dirName(somepath) + fileName(somepath) will always be equal to somepath.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. -
fileName
Returns the file component of specified filename. If no filename exists or the specified string is null, returns the empty string "". The path separator in the specified string is always assumed to be /. If on a platform where this is not the case, the specified string should be modified to contain "/" before invoking this method. The returned file name does not contain the preceding "/". -
split
Splits the string using the specified delimiters and returns the splits parts in a List. UseString.split(String, int)instead for greater options.- Parameters:
str- the string to be tokenizeddelim- delimiter string, each character in the string will be used as a delimiter to use while tokenizing
-
join
-
capitalWord
Converts the specified String to start with a capital letter. Only the first character is made uppercase, the rest of the specified string is not affected. -
sentenceCase
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. The characters in the delimiter string are used to delimit words in the sentence. If the delimiter string is null, the original string is returned as-is.
A runtime exception will be thrown if the specified string was null. -
viewableAscii
Converts non printable ascii characters in the specified String to escaped or readable equivalents. For example, a newline is converted to the sequence \n and say, ascii character 29 is converted to the sequence of chars: 'ascii(29)'.If the specified String is null, this method returns null.
- Parameters:
str- the String to convert- Returns:
- the converted String
-
viewableAscii
A version ofviewableAscii(java.lang.String)that takes a single char as a parameter.- Parameters:
char- the char to convert- Returns:
- the ascii readable char
-
arrayToString
Converts a character array into a viewable comma delimited string. Non-printable/readable characters are shown as readable equivalents. -
listToString
Converts a list into a string, each item being seperated by the specified delimiter. Each item in the list is converted by invokigntoStringon 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(java.util.List,java.lang.String,java.lang.String,java.lang.String) -
listToString
Converts a list into a string, each item being seperated by the specified delimiter. Each item in the list is converted by invokigntoStringon that item so the specified delimiter only applies to the outermost level.The converted string start and ends with the specified chars as well.
-
escapeSingleQuotes
Escapes all single quotes in the specified a string with a backslash character. -
escapeSingleQuotes
Escapes all single quotes in the specified a string with the specified escape character. So, if the specified escape character is ', all occurrences of o'tis the ele'phant becomes o''tis the ele''phant -
escapeDoubleQuotes
Escapes all double quotes in the specified a string with a backslash character. -
escapeDoubleQuotes
Escapes all double quotes in the specified a string with the specified escape character. So, if the specified escape character is \, all occurrences of o"tis the ele"phant becomes o\"tis the ele\"phant -
main
-