Class StringTools

java.lang.Object
org.sm.smtools.util.StringTools

public final class StringTools
extends java.lang.Object
The 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 a String[] array of comma-separated values.
    static java.lang.String[] convertToCSV​(java.lang.String source, char splitChar)
    Returns the input converted to a String[] 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 given String.
    static int getMaxLength​(java.lang.String[] strings)
    Calculates the largest length of all Strings 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 a String containing an int 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 kEOLCharacterSequence
      The 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 left
      nrOfCharacters - the minimum length of the string after the alignment
      padCharacter - 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 right
      nrOfCharacters - the minimum length of the string after the alignment
      padCharacter - 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 centered
      nrOfCharacters - the minimum length of the string after the centering
      padCharacter - 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 truncated
      nrOfCharacters - 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 left
      nrOfCharacters - the maximum length of the string after the aggressive alignment
      padCharacter - 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 right
      nrOfCharacters - the maximum length of the string after the aggressive alignment
      padCharacter - 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 centered
      nrOfCharacters - the maximum length of the string after the aggressive centering
      padCharacter - 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 all Strings in an array.
      Parameters:
      strings - the array of Strings to search the largest length of
      Returns:
      the largest length of all Strings 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 line
      lineCharacter - 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 given String.
      Parameters:
      stringToProcess - the String 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 optional locale parameter. For example, to use the Belgian comma as a separator instead of the dot, specify new Locale("be") as a parameter.

      Parameters:
      number - the double to convert to a string
      nrOfDecimals - the number of decimals in the resulting string
      locale - an optional parameter specifying the locale to be used (default is Local.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 optional locale parameter. For example, to use the Belgian comma as a separator instead of the dot, specify new Locale("be") as a parameter.

      Parameters:
      a - the real part of the complex number to convert to a string
      b - the imaginary part of the complex number to convert to a string
      nrOfDecimals - the number of decimals in the resulting string
      locale - an optional parameter specifying the locale to be used (default is Local.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 optional locale parameter. For example, to use the Belgian comma as a separator instead of the dot, specify new Locale("be") as a parameter.

      Parameters:
      c - the complex number to convert to a string
      nrOfDecimals - the number of decimals in the resulting string
      locale - an optional parameter specifying the locale to be used (default is Local.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 format
      locale - the locale to use
      Returns:
      the formatted number
    • paddLeadingZeros

      public static java.lang.String paddLeadingZeros​(int number, int minFieldWidth)
      Returns a String containing an int padded with leading zeros.
      Parameters:
      number - the number to padd
      minFieldWidth - 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 - the String 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 - the String 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 - the String to reverse
      beginIndex - the begin index
      endIndex - 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 - the String 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 - the String to convert the tabs of
      nrOfSpacesPerTab - [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 - the String to convert the spaces of
      nrOfSpacesPerTab - [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 - the String 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 a String[] array of comma-separated values.

      The CSV parser can operate on quoted, unquoted and empty Strings.

      Parameters:
      source - the source String
      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 a String[] array of comma-separated values with a specified split character.

      The CSV parser can operate on quoted, unquoted and empty Strings.

      Parameters:
      source - the source String
      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 input String
      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