Class StringTools
public final class StringTools
extends java.lang.Object
StringTools
class is mainly intended for string alignment operations.
Using this class, strings can be aligned left, right or centered. Aggressive alignment is also supported, in the sense that any string excess is truncated after alignment.
Furthermore, lines of characters can be created (e.g., dashed lines) and doubles can be converted to strings (with respect to a certain number of decimals).
All methods in this class are static, so they should be invoked as:
... = StringTools.method(...);
Note that this class cannot be subclassed!
- Version:
- 27/03/2020
- Author:
- Sven Maerivoet
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
kEOLCharacterSequence
The platform-dependent end-of-line (EOL) character sequence. -
Method Summary
Modifier and Type Method Description static java.lang.String
aggressiveAlignCenter(java.lang.String stringToCenter, int nrOfCharacters, char padCharacter)
Aggressively centers a string, padding with extra characters if necessary.static java.lang.String
aggressiveAlignLeft(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)
Aggressively aligns a string left, padding with extra characters if necessary.static java.lang.String
aggressiveAlignRight(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)
Aggressively aligns a string right, padding with extra characters if necessary.static java.lang.String
alignCenter(java.lang.String stringToCenter, int nrOfCharacters, char padCharacter)
Centers a string, padding with extra characters if necessary.static java.lang.String
alignLeft(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)
Aligns a string left, padding with extra characters if necessary.static java.lang.String
alignRight(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)
Aligns a string right, padding with extra characters if necessary.static java.lang.String
capitaliseFirstLetter(java.lang.String text)
Capitalises the first letter in a string.static java.lang.String
convertComplexNumberToString(double a, double b, int nrOfDecimals, java.util.Locale... locale)
Creates a string representation of the specified complex number.static java.lang.String
convertComplexNumberToString(ComplexNumber c, int nrOfDecimals, java.util.Locale... locale)
Creates a string representation of the specified complex number.static java.lang.String
convertDoubleToString(double number, int nrOfDecimals, java.util.Locale... locale)
Creates a string representation of the specified double.static java.lang.String
convertEOLsToStrings(java.lang.String input)
Converts all system-dependent end-of-line (EOL) character sequences into readable string representations of \r\n.static java.lang.String
convertSpacesToTabs(java.lang.String input, int... nrOfSpacesPerTab)
Converts all spaces in a string to tabs.static java.lang.String
convertStringsToEOLs(java.lang.String input)
Converts all readable string representations of \r\n into system-dependent end-of-line (EOL) character sequences.static java.lang.String
convertTabsToSpaces(java.lang.String input, int... nrOfSpacesPerTab)
Converts all tabs in a string to spaces.static java.lang.String[]
convertToCSV(java.lang.String source)
Returns the input converted to aString[]
array of comma-separated values.static java.lang.String[]
convertToCSV(java.lang.String source, char splitChar)
Returns the input converted to aString[]
array of comma-separated values with a specified split character.static java.lang.String
createLineOfCharacters(int length, char lineCharacter)
Creates a string containing a repetition of a character.static java.lang.String
formatInteger(int number)
Formats an integer with thousand-, million-, ...static java.lang.String
formatInteger(int number, java.util.Locale locale)
Formats an integer with thousand-, million-, ...static int
getDoublePrecision(double x)
Returns the precision of the specified number, based on a leading zero and the number of zero decimals directly following the decimal point.static java.lang.String
getIndentation(java.lang.String stringToProcess)
Returns the optional indentation (i.e., any whitespace at the beginning) of a givenString
.static int
getMaxLength(java.lang.String[] strings)
Calculates the largest length of allString
s in an array.static java.lang.String
getSoundex(java.lang.String input)
Returns the Soundex code of the provided string.static boolean
isComment(java.lang.String input)
Checks whether or not a line contains a comment.static java.lang.String
paddLeadingZeros(int number, int minFieldWidth)
Returns aString
containing anint
padded with leading zeros.static void
printExit(java.lang.String data)
Prints the provided data via the output stream and terminates the program.static java.lang.String
reverse(java.lang.String input)
Reverses all the characters in a string.static java.lang.String
substring(java.lang.String input, int beginIndex, int endIndex)
Extracts a substring from a string.static java.lang.String
truncate(java.lang.String stringToTruncate, int nrOfCharacters)
Truncates the end of a string.Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
kEOLCharacterSequence
public static final java.lang.String kEOLCharacterSequenceThe platform-dependent end-of-line (EOL) character sequence.
-
-
Method Details
-
alignLeft
public static java.lang.String alignLeft(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)Aligns a string left, padding with extra characters if necessary.- Parameters:
stringToAlign
- the string that should be aligned leftnrOfCharacters
- the minimum length of the string after the alignmentpadCharacter
- the character used to pad the string at the right end- Returns:
- the left aligned string
-
alignRight
public static java.lang.String alignRight(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)Aligns a string right, padding with extra characters if necessary.- Parameters:
stringToAlign
- the string that should be aligned rightnrOfCharacters
- the minimum length of the string after the alignmentpadCharacter
- the character used to pad the string at the left end- Returns:
- the right aligned string
-
alignCenter
public static java.lang.String alignCenter(java.lang.String stringToCenter, int nrOfCharacters, char padCharacter)Centers a string, padding with extra characters if necessary.- Parameters:
stringToCenter
- the string that should be centerednrOfCharacters
- the minimum length of the string after the centeringpadCharacter
- the character used to pad the string at the left and right ends- Returns:
- the centered string
-
truncate
public static java.lang.String truncate(java.lang.String stringToTruncate, int nrOfCharacters)Truncates the end of a string.- Parameters:
stringToTruncate
- the string that should be truncatednrOfCharacters
- the maximum length of the string after the truncation- Returns:
- the truncated string
-
aggressiveAlignLeft
public static java.lang.String aggressiveAlignLeft(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)Aggressively aligns a string left, padding with extra characters if necessary.If the length of the resulting string is too long, the end is truncated.
- Parameters:
stringToAlign
- the string that should be aggressively aligned leftnrOfCharacters
- the maximum length of the string after the aggressive alignmentpadCharacter
- the character used to pad the string at the right end- Returns:
- the left aggressively aligned string
- See Also:
alignLeft(String,int,char)
,truncate(String,int)
-
aggressiveAlignRight
public static java.lang.String aggressiveAlignRight(java.lang.String stringToAlign, int nrOfCharacters, char padCharacter)Aggressively aligns a string right, padding with extra characters if necessary.If the length of the resulting string is too long, the end is truncated.
- Parameters:
stringToAlign
- the string that should be aggressively aligned rightnrOfCharacters
- the maximum length of the string after the aggressive alignmentpadCharacter
- the character used to pad the string at the left end- Returns:
- the right aggressively aligned string
- See Also:
alignRight(String,int,char)
,truncate(String,int)
-
aggressiveAlignCenter
public static java.lang.String aggressiveAlignCenter(java.lang.String stringToCenter, int nrOfCharacters, char padCharacter)Aggressively centers a string, padding with extra characters if necessary.If the length of the resulting string is too long, the end is truncated.
- Parameters:
stringToCenter
- the string that should be aggressively centerednrOfCharacters
- the maximum length of the string after the aggressive centeringpadCharacter
- the character used to pad the string at the left and right ends- Returns:
- the aggressively centered string
- See Also:
alignCenter(String,int,char)
,truncate(String,int)
-
getMaxLength
public static int getMaxLength(java.lang.String[] strings)Calculates the largest length of allString
s in an array.- Parameters:
strings
- the array ofString
s to search the largest length of- Returns:
- the largest length of all
String
s in the specified array
-
capitaliseFirstLetter
public static java.lang.String capitaliseFirstLetter(java.lang.String text)Capitalises the first letter in a string.- Parameters:
text
- the text to capitalise the first letter of- Returns:
- the text with the first letter capitalised
-
createLineOfCharacters
public static java.lang.String createLineOfCharacters(int length, char lineCharacter)Creates a string containing a repetition of a character.As an example, a dashed line of length 5 can be created as:
StringTools.createLineOfCharacters(5,'-')
The result will be "-----".
- Parameters:
length
- the total length of the linelineCharacter
- the character that is used to create the line- Returns:
- a line containing the repetition of the specified character
-
getIndentation
public static java.lang.String getIndentation(java.lang.String stringToProcess)Returns the optional indentation (i.e., any whitespace at the beginning) of a givenString
.- Parameters:
stringToProcess
- theString
to process- Returns:
- the optional indentation
-
convertDoubleToString
public static java.lang.String convertDoubleToString(double number, int nrOfDecimals, java.util.Locale... locale)Creates a string representation of the specified double.Some examples:
StringTools.convertDoubleToString(5.2,3)
results in "5.200".StringTools.convertDoubleToString(5.6,0)
results in "6" (rounding occurs!).
The default locale to be is
Locale.UK
; this can be changed by specifying an optionallocale
parameter. For example, to use the Belgian comma as a separator instead of the dot, specifynew Locale("be")
as a parameter.- Parameters:
number
- the double to convert to a stringnrOfDecimals
- the number of decimals in the resulting stringlocale
- an optional parameter specifying the locale to be used (default isLocal.UK
)- Returns:
- a string representation of the specified double
-
convertComplexNumberToString
public static java.lang.String convertComplexNumberToString(double a, double b, int nrOfDecimals, java.util.Locale... locale)Creates a string representation of the specified complex number.The default locale to be is
Locale.UK
; this can be changed by specifying an optionallocale
parameter. For example, to use the Belgian comma as a separator instead of the dot, specifynew Locale("be")
as a parameter.- Parameters:
a
- the real part of the complex number to convert to a stringb
- the imaginary part of the complex number to convert to a stringnrOfDecimals
- the number of decimals in the resulting stringlocale
- an optional parameter specifying the locale to be used (default isLocal.UK
)- Returns:
- a string representation of the specified complex number
-
convertComplexNumberToString
public static java.lang.String convertComplexNumberToString(ComplexNumber c, int nrOfDecimals, java.util.Locale... locale)Creates a string representation of the specified complex number.The default locale to be is
Locale.UK
; this can be changed by specifying an optionallocale
parameter. For example, to use the Belgian comma as a separator instead of the dot, specifynew Locale("be")
as a parameter.- Parameters:
c
- the complex number to convert to a stringnrOfDecimals
- the number of decimals in the resulting stringlocale
- an optional parameter specifying the locale to be used (default isLocal.UK
)- Returns:
- a string representation of the specified complex number
-
getDoublePrecision
public static int getDoublePrecision(double x)Returns the precision of the specified number, based on a leading zero and the number of zero decimals directly following the decimal point.- Parameters:
x
- the number to calculate the precision of- Returns:
- the precision of the specified number
-
formatInteger
public static java.lang.String formatInteger(int number)Formats an integer with thousand-, million-, ... separators using the default locale.- Parameters:
number
- the number to format- Returns:
- the formatted number
-
formatInteger
public static java.lang.String formatInteger(int number, java.util.Locale locale)Formats an integer with thousand-, million-, ... separators using a specified locale.- Parameters:
number
- the number to formatlocale
- the locale to use- Returns:
- the formatted number
-
paddLeadingZeros
public static java.lang.String paddLeadingZeros(int number, int minFieldWidth)Returns aString
containing anint
padded with leading zeros.- Parameters:
number
- the number to paddminFieldWidth
- the minimal number of characters in the field- Returns:
- a
String
containing the specified number padded with leading zeros
-
convertEOLsToStrings
public static java.lang.String convertEOLsToStrings(java.lang.String input)Converts all system-dependent end-of-line (EOL) character sequences into readable string representations of \r\n.- Parameters:
input
- theString
to convert all system-dependent EOL character sequences of- Returns:
- a
String
with all EOL character sequences converted into readable string representations of \r\n
-
convertStringsToEOLs
public static java.lang.String convertStringsToEOLs(java.lang.String input)Converts all readable string representations of \r\n into system-dependent end-of-line (EOL) character sequences.- Parameters:
input
- theString
with all readable string representations of \r\n to convert- Returns:
- a
String
with all string representations converted into character sequences of system-dependent end-of-line (EOL) character sequences
-
substring
public static java.lang.String substring(java.lang.String input, int beginIndex, int endIndex)Extracts a substring from a string. The end index is truncated suitable if it is beyond the string's length.- Parameters:
input
- theString
to reversebeginIndex
- the begin indexendIndex
- the end index- Returns:
- a
String
with all characters from the original one reversed
-
reverse
public static java.lang.String reverse(java.lang.String input)Reverses all the characters in a string.- Parameters:
input
- theString
to reverse- Returns:
- a
String
with all characters from the original one reversed
-
convertTabsToSpaces
public static java.lang.String convertTabsToSpaces(java.lang.String input, int... nrOfSpacesPerTab)Converts all tabs in a string to spaces.The number of spaces to use per tab is optional, it is 2 by default.
- Parameters:
input
- theString
to convert the tabs ofnrOfSpacesPerTab
- [OPTIONAL] the number of spaces to use when replacing each tab- Returns:
- a
String
with all tabs converted to spaces
-
convertSpacesToTabs
public static java.lang.String convertSpacesToTabs(java.lang.String input, int... nrOfSpacesPerTab)Converts all spaces in a string to tabs.The number of spaces to use per tab is optional, it is 2 by default.
- Parameters:
input
- theString
to convert the spaces ofnrOfSpacesPerTab
- [OPTIONAL] the number of spaces to use when replacing them by a tab- Returns:
- a
String
with all spaces converted to tabs
-
isComment
public static boolean isComment(java.lang.String input)Checks whether or not a line contains a comment.Comments are preceeded by a hash-sign (#).
- Parameters:
input
- theString
to check- Returns:
- a
boolean
indicating whether or not the line contains a comment
-
convertToCSV
public static java.lang.String[] convertToCSV(java.lang.String source)Returns the input converted to aString[]
array of comma-separated values.The CSV parser can operate on quoted, unquoted and empty
String
s.- Parameters:
source
- the sourceString
- Returns:
- the input converted to a
String[]
array of comma-separated values
-
convertToCSV
public static java.lang.String[] convertToCSV(java.lang.String source, char splitChar)Returns the input converted to aString[]
array of comma-separated values with a specified split character.The CSV parser can operate on quoted, unquoted and empty
String
s.- Parameters:
source
- the sourceString
splitChar
- the character used to split the CSV record- Returns:
- the input converted to a
String[]
array of comma-separated values
-
getSoundex
public static java.lang.String getSoundex(java.lang.String input)Returns the Soundex code of the provided string. The goal is to hash words (typically surnames) into a smaller space using a simple model which approximates the sound of the word when spoken by an English speaker. Each word is reduced to a four-character string, the first character being a upper case letter and the remaining being three digits. Double letters are collapsed to a single digit.The algorithm makes no distinction between lower- and uppercase characters.
- Parameters:
input
- the inputString
- Returns:
- the Soundex code corresponding to the provided input string
-
printExit
public static void printExit(java.lang.String data)Prints the provided data via the output stream and terminates the program.- Parameters:
data
- the data to print
-