Class StringTools
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:
- 18/02/2022
- Author:
- Sven Maerivoet
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The platform-dependent end-of-line (EOL) character sequence. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
aggressiveAlignCenter
(String stringToCenter, int nrOfCharacters, char padCharacter) Aggressively centers a string, padding with extra characters if necessary.static String
aggressiveAlignLeft
(String stringToAlign, int nrOfCharacters, char padCharacter) Aggressively aligns a string left, padding with extra characters if necessary.static String
aggressiveAlignRight
(String stringToAlign, int nrOfCharacters, char padCharacter) Aggressively aligns a string right, padding with extra characters if necessary.static String
alignCenter
(String stringToCenter, int nrOfCharacters, char padCharacter) Centers a string, padding with extra characters if necessary.static String
Aligns a string left, padding with extra characters if necessary.static String
alignRight
(String stringToAlign, int nrOfCharacters, char padCharacter) Aligns a string right, padding with extra characters if necessary.static String
Anonymises the input by applying a SHA-256 hash followed by a Base64 encoding.static String
capitaliseFirstLetter
(String text) Capitalises the first letter in a string.static String
convertComplexNumberToString
(double a, double b, int nrOfDecimals, Locale... locale) Creates a string representation of the specified complex number.static String
convertComplexNumberToString
(ComplexNumber c, int nrOfDecimals, Locale... locale) Creates a string representation of the specified complex number.static String
convertDoubleToString
(double number, int nrOfDecimals, Locale... locale) Creates a string representation of the specified double.static String
convertEOLsToStrings
(String input) Converts all system-dependent end-of-line (EOL) character sequences into readable string representations of \r\n.static String
convertPercentageToString
(int numerator, int denominator, int... nrOfDecimals) Calculates a percentage and converts it to aString
with an optionally specified precision.static String
convertSpacesToTabs
(String input, int... nrOfSpacesPerTab) Converts all spaces in a string to tabs.static String
convertStringsToEOLs
(String input) Converts all readable string representations of \r\n into system-dependent end-of-line (EOL) character sequences.static String
convertTabsToSpaces
(String input, int... nrOfSpacesPerTab) Converts all tabs in a string to spaces.static String[]
convertToCSV
(String source) Returns the input converted to aString[]
array of comma-separated values.static String[]
convertToCSV
(String source, char splitChar) Returns the input converted to aString[]
array of comma-separated values with a specified split character.static String
createLineOfCharacters
(int length, char lineCharacter) Creates a string containing a repetition of a character.static String
formatInteger
(int number) Formats an integer with thousand-, million-, ... separators using the default locale.static String
formatInteger
(int number, Locale locale) Formats an integer with thousand-, million-, ... separators using a specified locale.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 String
getIndentation
(String stringToProcess) Returns the optional indentation (i.e., any whitespace at the beginning) of a givenString
.static int
getMaxLength
(String[] strings) Calculates the largest length of allString
s in an array.static String
getSoundex
(String input) Returns the Soundex code of the provided string.static boolean
Checks whether or not a line contains a comment.static boolean
isRegExCharacter
(char c) Returns whether or not a specified character is considered as a regex-modifier in Java, i.e.static String
paddLeadingZeros
(int number, int minFieldWidth) Returns aString
containing anint
padded with leading zeros.static void
Prints the provided data via the output stream and terminates the program.static String
Reverses all the characters in a string.static String
Extracts a substring from a string.static String
Truncates the end of a string.
-
Field Details
-
kEOLCharacterSequence
The platform-dependent end-of-line (EOL) character sequence.
-
-
Method Details
-
alignLeft
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
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
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
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 String aggressiveAlignLeft(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:
-
aggressiveAlignRight
public static String aggressiveAlignRight(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:
-
aggressiveAlignCenter
public static String aggressiveAlignCenter(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:
-
getMaxLength
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
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
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
Returns the optional indentation (i.e., any whitespace at the beginning) of a givenString
.- Parameters:
stringToProcess
- theString
to process- Returns:
- the optional indentation
-
convertDoubleToString
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
-
convertPercentageToString
Calculates a percentage and converts it to aString
with an optionally specified precision.- Parameters:
numerator
- the numeratordenominator
- the denominator- Returns:
- the percentage as a
String
-
convertComplexNumberToString
public static String convertComplexNumberToString(double a, double b, int nrOfDecimals, 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 String convertComplexNumberToString(ComplexNumber c, int nrOfDecimals, 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
Formats an integer with thousand-, million-, ... separators using the default locale.- Parameters:
number
- the number to format- Returns:
- the formatted number
-
formatInteger
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
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
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
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
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
Reverses all the characters in a string.- Parameters:
input
- theString
to reverse- Returns:
- a
String
with all characters from the original one reversed
-
convertTabsToSpaces
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
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
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
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
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
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
-
isRegExCharacter
public static boolean isRegExCharacter(char c) Returns whether or not a specified character is considered as a regex-modifier in Java, i.e.<([{\^-=$!|]})?*+.>
.
- Parameters:
c
- the character to test- Returns:
- a
boolean
indicating whether or not the specified character is considered as a regex-modifier in Java.
-
anonymise
Anonymises the input by applying a SHA-256 hash followed by a Base64 encoding.- Parameters:
input
- the input to be anonymised- Returns:
- the hashed input
-
printExit
Prints the provided data via the output stream and terminates the program.- Parameters:
data
- the data to print
-