Package info.freelibrary.util
Class StringUtils
- java.lang.Object
-
- info.freelibrary.util.StringUtils
-
public final class StringUtils extends Object
Provides a few convenience methods for working with strings.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
addLineNumbers(String aMessage)
Adds line numbers to any string.static String
format(String aMessage, Object... aDetail)
Formats the supplied message using the supplied details.static String
format(String aMessage, String... aDetails)
Takes aString
in the form "This is {} text {}" and replaces the{}
s with values from the suppliedString[]
.static boolean
isEmpty(String aString)
Returns true if the supplied string is null, empty, or contains nothing but whitespace.static String
joinKeys(Map<String,?> aMap, char aSeparator)
Turns the keys in a map into a character delimited string.static String
normalizeWS(String aMessage)
Normalizes white space in the message value.static String
padEnd(String aString, String aPadding, int aRepeatCount)
Pads the end of a supplied string with the repetition of a supplied value.static String
padStart(String aString, String aPadding, int aRepeatCount)
Pads the beginning of a supplied string with the repetition of a supplied value.static int[]
parseIntRange(String aIntRange)
Parses strings with an integer range (e.g., 2-5) and returns an expanded integer array {2, 3, 4, 5} with those values.static String
read(File aFile)
Reads the contents of a file into a string using the UTF-8 character set encoding.static String
read(File aFile, String aCharsetName)
Reads the contents of a file using the suppliedCharset
; the default system charset may vary across systems so can't be trusted.static String
read(File aFile, Charset aCharset)
Reads the contents of a file using the suppliedCharset
; the default system charset may vary across systems so can't be trusted.static String
repeat(char aChar, int aRepeatCount)
Creates a new string from the repetition of a supplied char.static String
repeat(String aValue, int aRepeatCount)
Creates a new string from the repetition of a supplied value.static String
reverse(String aString)
Reverses the characters in a string.static String
toCharCount(String aString, int aCount)
Formats a string with or without line breaks into a string with lines with less than a supplied number of characters per line.static String
toString(char aPadChar, Object... aVarargs)
A convenience method for toString(Object[], char) to add varargs support.static String
toString(int aInt)
Returns a human-friendly, locale dependent, string representation of the supplied int; for instance, "1" becomes "first", "2" becomes "second", etc.static String
toString(Object[] aObjArray, char aPadChar)
Concatenates the string representations of a series of objects (by calling theirObject.toString()
method).static String
toString(Map<String,String[]> aMap)
Provides a toString() method for maps that have string keys and string array values.static String[]
toStrings(Object... aVarargs)
Converts a varargs into an array of strings by callingtoString()
on each object.static String
toUpcaseString(int aInt)
Returns an up-cased human-friendly string representation for the supplied int; for instance, "1" becomes "First", "2" becomes "Second", etc.static String[]
trim(String... aStringArray)
Removes empty and null strings from a string array.static String
trimTo(String aString, String aDefault)
Trims a string; if there is nothing left after the trimming, returns whatever the default value passed in is.static boolean
trimToBool(String aString, boolean aBool)
Trims a string object down into a boolean and has the ability to define what the default value should be.static String
trimToNull(String aString)
Trims a string; if there is nothing left after the trimming, returns null.static String
upcase(String aString)
Upcases a string.
-
-
-
Method Detail
-
trimToNull
public static String trimToNull(String aString)
Trims a string; if there is nothing left after the trimming, returns null. If the passed in object is not a string, a cast exception will be thrown.- Parameters:
aString
- The string to be trimmed- Returns:
- The trimmed string or null if string is empty
-
trimToBool
public static boolean trimToBool(String aString, boolean aBool)
Trims a string object down into a boolean and has the ability to define what the default value should be. We only offer the method with the default value because most times a boolean with either exist or not (and in the case of not a default should be specified).- Parameters:
aString
- A boolean in string formaBool
- A default boolean value- Returns:
- The boolean representation of the string value or the default value
-
trimTo
public static String trimTo(String aString, String aDefault)
Trims a string; if there is nothing left after the trimming, returns whatever the default value passed in is.- Parameters:
aString
- The string to be trimmedaDefault
- A default string to return if a null string is passed in- Returns:
- The trimmed string or the default value if string is empty
-
format
public static String format(String aMessage, Object... aDetail)
Formats the supplied message using the supplied details. The string form of the detail comes from the object'stoString()
method.- Parameters:
aMessage
- A message to formataDetail
- Additional details to integrate into the message- Returns:
- The formatted message
-
format
public static String format(String aMessage, String... aDetails)
Takes aString
in the form "This is {} text {}" and replaces the{}
s with values from the suppliedString[]
. The number of curly braces should be the same as the number of strings in the string array.- Parameters:
aMessage
- A string that contains curly braces in the form{}
aDetails
- Strings that should be put in place of the curly braces in the message string.- Returns:
- The formatted string
-
toStrings
public static String[] toStrings(Object... aVarargs)
Converts a varargs into an array of strings by callingtoString()
on each object.- Parameters:
aVarargs
- A varargs of objects- Returns:
- An array of strings
-
normalizeWS
public static String normalizeWS(String aMessage)
Normalizes white space in the message value.- Parameters:
aMessage
- A message- Returns:
- The message with white space normalized
-
padStart
public static String padStart(String aString, String aPadding, int aRepeatCount)
Pads the beginning of a supplied string with the repetition of a supplied value.- Parameters:
aString
- The string to padaPadding
- The string to be repeated as the paddingaRepeatCount
- How many times to repeat the padding- Returns:
- The front padded string
-
padEnd
public static String padEnd(String aString, String aPadding, int aRepeatCount)
Pads the end of a supplied string with the repetition of a supplied value.- Parameters:
aString
- The string to padaPadding
- The string to be repeated as the paddingaRepeatCount
- How many times to repeat the padding- Returns:
- The end padded string
-
toCharCount
public static String toCharCount(String aString, int aCount)
Formats a string with or without line breaks into a string with lines with less than a supplied number of characters per line.- Parameters:
aString
- A string to formataCount
- A number of characters to allow per line- Returns:
- A string formatted using the supplied count
-
repeat
public static String repeat(String aValue, int aRepeatCount)
Creates a new string from the repetition of a supplied value.- Parameters:
aValue
- The string to repeat, creating a new stringaRepeatCount
- The number of times to repeat the supplied value- Returns:
- The new string containing the supplied value repeated the specified number of times
-
repeat
public static String repeat(char aChar, int aRepeatCount)
Creates a new string from the repetition of a supplied char.- Parameters:
aChar
- The char to repeat, creating a new stringaRepeatCount
- The number of times to repeat the supplied value- Returns:
- The new string containing the supplied value repeated the specified number of times
-
read
public static String read(File aFile, String aCharsetName) throws IOException
Reads the contents of a file using the suppliedCharset
; the default system charset may vary across systems so can't be trusted.- Parameters:
aFile
- The file from which to readaCharsetName
- The name of the character set of the file to be read- Returns:
- The information read from the file
- Throws:
IOException
- If the supplied file could not be read
-
read
public static String read(File aFile, Charset aCharset) throws IOException
Reads the contents of a file using the suppliedCharset
; the default system charset may vary across systems so can't be trusted.- Parameters:
aFile
- The file from which to readaCharset
- The character set of the file to be read- Returns:
- The information read from the file
- Throws:
IOException
- If the supplied file could not be read
-
read
public static String read(File aFile) throws IOException
Reads the contents of a file into a string using the UTF-8 character set encoding.- Parameters:
aFile
- The file from which to read- Returns:
- The information read from the file
- Throws:
IOException
- If the supplied file could not be read
-
trim
public static String[] trim(String... aStringArray)
Removes empty and null strings from a string array.- Parameters:
aStringArray
- A varargs that may contain empty or null strings- Returns:
- A string array without empty or null strings
-
isEmpty
public static boolean isEmpty(String aString)
Returns true if the supplied string is null, empty, or contains nothing but whitespace.- Parameters:
aString
- A string to test to see if it is null, empty or contains nothing but whitespace- Returns:
- True if the supplied string is empty; else, false
-
toString
public static String toString(char aPadChar, Object... aVarargs)
A convenience method for toString(Object[], char) to add varargs support.- Parameters:
aPadChar
- A padding characteraVarargs
- A varargs into which to insert the padding character- Returns:
- A string form of the varargs with padding added
-
toString
public static String toString(Object[] aObjArray, char aPadChar)
Concatenates the string representations of a series of objects (by calling theirObject.toString()
method). Concatenated strings are separated using the supplied 'padding' character.- Parameters:
aObjArray
- An array of objects whosetoString()
representations should be concatenatedaPadChar
- The character used to separate concatenated strings- Returns:
- A concatenation of the supplied objects' string representations
-
joinKeys
public static String joinKeys(Map<String,?> aMap, char aSeparator)
Turns the keys in a map into a character delimited string. The order is only consistent if the map is sorted.- Parameters:
aMap
- The map from which to pull the keysaSeparator
- The character separator for the construction of the string- Returns:
- A string constructed from the keys in the map
-
toString
public static String toString(Map<String,String[]> aMap)
Provides a toString() method for maps that have string keys and string array values. The regular map toString() works fine for string keys and string values but, since a string array doesn't have a toString(), the map's toString() method doesn't produce a useful output. This fixes that.- Parameters:
aMap
- A map of string keys and string array values to turn into a single string representation of the map- Returns:
- A concatenation of the supplied map's string values
-
addLineNumbers
public static String addLineNumbers(String aMessage)
Adds line numbers to any string. This is useful when I need to debug an XQuery outside the context of an IDE (i.e., in debugging output).- Parameters:
aMessage
- Text to which line numbers should be added- Returns:
- The supplied text with line numbers at the first of each line
-
parseIntRange
public static int[] parseIntRange(String aIntRange)
Parses strings with an integer range (e.g., 2-5) and returns an expanded integer array {2, 3, 4, 5} with those values.- Parameters:
aIntRange
- A string representation of a range of integers- Returns:
- An int array with the expanded values of the string representation
-
toString
public static String toString(int aInt)
Returns a human-friendly, locale dependent, string representation of the supplied int; for instance, "1" becomes "first", "2" becomes "second", etc.- Parameters:
aInt
- An int to convert into a string- Returns:
- The string form of the supplied int
-
reverse
public static String reverse(String aString)
Reverses the characters in a string.- Parameters:
aString
- A string whose characters are to be reversed- Returns:
- A string with the supplied string reversed
-
upcase
public static String upcase(String aString)
Upcases a string.- Parameters:
aString
- A string to upcase- Returns:
- The upcased string
-
toUpcaseString
public static String toUpcaseString(int aInt)
Returns an up-cased human-friendly string representation for the supplied int; for instance, "1" becomes "First", "2" becomes "Second", etc.- Parameters:
aInt
- An integer to convert into a string- Returns:
- The string form of the supplied integer
-
-