Class StringUtils

java.lang.Object
com.infinitekind.util.StringUtils

public class StringUtils
extends java.lang.Object
Useful string manipulation functions
  • Constructor Summary

    Constructors 
    Constructor Description
    StringUtils()  
  • Method Summary

    Modifier and Type Method Description
    static boolean areStringsIdentical​(java.lang.String s1, java.lang.String s2)  
    static java.lang.String base64Encode​(byte[] bytes)
    Encode the given byte array using base64 and return the result as a string.
    static java.lang.String base64Encode​(java.lang.String str)
    Encode the given string as a (utf8) byte array and then encode that using base64 and return the result as a string.
    static java.lang.String capitalizeFirst​(java.lang.String string)
    Return a String with the first letter capitalized.
    static int computeLevenshteinDistance​(java.lang.CharSequence str1, java.lang.CharSequence str2)  
    static java.lang.String convertToFileName​(java.lang.String str)
    Performs a conversion from the given string to an encoded version that can be used as a file/URL name.
    static int countFields​(java.lang.String str, char delimiter)  
    static java.lang.String decode​(java.lang.String str)  
    static byte[] decodeHex​(java.lang.String s)  
    static java.lang.String decodeURL​(java.lang.String str)
    This method decodes a URL parameter that has special characters %encoded
    static void doNothing()  
    static java.lang.String encode​(java.lang.String str)  
    static java.lang.String encodeHex​(byte[] buf, boolean breaklines)  
    static java.lang.String fieldIndex​(java.lang.String str, char delimiter, int n)  
    static java.lang.String fillLeft​(java.lang.String str, int width, char fillChar)  
    static java.lang.String fillRight​(java.lang.String str, int width, char fillChar)  
    static java.lang.String formatFixedDecimals​(java.lang.String value, char decimalChar, int numDecimals)
    Formats the given double [0..1.00] as a percentage with the specified number of decimal digits showing.
    static java.lang.String formatMinDecimals​(java.lang.String value, char decimalChar, int numDecimals)
    Formats the given double [0..1.00] as a percentage with the specified number of decimal digits showing as a minimum, there could be more.
    static java.lang.String formatPercentage​(double value, char decimalChar)
    Formats the given double [0..1.00] as a percentage with either zero (if an even percentage) or two decimal digits showing.
    static java.lang.String formatPercentage​(double value, char decimalChar, boolean forceDecimal)
    Formats the given double [0..1.00] as a percentage with either zero (if an even percentage) or two decimal digits showing.
    static java.lang.String formatRate​(double rate, char decimalChar)  
    static java.lang.String formatShortRate​(double rate, char decimalChar)  
    static java.lang.String getExtensionFromFilename​(java.lang.String fileName)
    Returns the extension, if any, on the given file name.
    static java.lang.String getGMTDateStr​(java.util.Date dt)  
    static java.lang.String getNLengthString​(int width, char fillChar)  
    static java.lang.String getNonNull​(java.lang.String str)
    Convenience method that Returns the given string or the empty string if the given string is null
    static char guessDecimalType​(java.lang.String currencyString)  
    static boolean isAllNumber​(java.lang.String str)  
    static boolean isBlank​(java.lang.String candidate)
    Null safe check for a String with nothing but whitespace characters.
    static boolean isDate​(java.lang.String str)  
    static boolean isEmpty​(java.lang.String candidate)
    Null safe check for "" (empty string).
    static boolean isInteger​(java.lang.String str)  
    static boolean isNumeric​(java.lang.String str)  
    static java.lang.String join​(java.util.List<java.lang.String> strings, java.lang.String joinString)
    Concatenate all strings in the given list with the joinString between them
    static boolean looksLikeFormula​(java.lang.String expr)  
    static void main​(java.lang.String[] argv)  
    static double parseDouble​(java.lang.String val, char decimalChar)  
    static double parseDouble​(java.lang.String val, double defaultVal, char decimalChar)  
    static double parseDoubleWithException​(java.lang.String val, char decimalChar)  
    static double parseFormula​(java.lang.String str, char dec)  
    static double parseFraction​(java.lang.String fraction, char decimalChar)
    Parse a fraction from a string - ie "1/5".
    static long parseQuickenDate​(java.lang.String date)  
    static double parseRate​(java.lang.String rateStr, char decimalChar)  
    static double parseRate​(java.lang.String rateStr, double defaultRate, char decimalChar)  
    static void parseURLParameters​(java.lang.String paramStr, java.util.Map<java.lang.String,​java.lang.String> map)  
    static java.lang.String replaceAll​(java.lang.String str, java.lang.String toReplace, java.lang.String replaceWith)
    Replace one or more tokens inside another string with a value.
    static java.lang.String shortenStringIfLongerThan​(java.lang.String s, int limit, java.lang.String suffix)
    Returns a substring whole length is guaranteed to be no longer than limit or the given string itself if its length is less than limit.
    static void sortStringArray​(java.lang.String[] array)  
    static java.lang.String[] split​(java.lang.String str, char splitChar)  
    static java.util.List<java.lang.String> splitIntoList​(java.lang.String str, char splitChar)  
    static java.lang.String stripExtension​(java.lang.String fileName)
    Returns the filename passed as argument without extension
    static java.lang.String stripExtraCharacters​(java.lang.String str)
    Remove leading and trailing quotes or white space from a string
    static java.lang.String stripNonFilenameSafeCharacters​(java.lang.String filename)
    Strip all characters that should not appear in a filename (including path characters)
    static java.lang.String stripNonNumbers​(java.lang.String str, char decimalChar)  
    static java.lang.String stripPath​(java.lang.String filename)
    Returns the file name with any path stripped from the beginning.
    static java.lang.String truncateHead​(java.lang.String s, int limit, java.lang.String prefix)
    Returns a string that is guaranteed to be withing 'limit' characters by truncating the beginning of the string, if necessary.
    static java.lang.String[] vectorToStringArray​(java.util.Vector v)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • encodeHex

      public static final java.lang.String encodeHex​(byte[] buf, boolean breaklines)
    • decodeHex

      public static final byte[] decodeHex​(java.lang.String s)
    • shortenStringIfLongerThan

      public static java.lang.String shortenStringIfLongerThan​(java.lang.String s, int limit, java.lang.String suffix)
      Returns a substring whole length is guaranteed to be no longer than limit or the given string itself if its length is less than limit.
    • truncateHead

      public static java.lang.String truncateHead​(java.lang.String s, int limit, java.lang.String prefix)
      Returns a string that is guaranteed to be withing 'limit' characters by truncating the beginning of the string, if necessary.
    • base64Encode

      public static final java.lang.String base64Encode​(java.lang.String str)
      Encode the given string as a (utf8) byte array and then encode that using base64 and return the result as a string. Note: Only available as of build 528
    • base64Encode

      public static final java.lang.String base64Encode​(byte[] bytes)
      Encode the given byte array using base64 and return the result as a string.
    • splitIntoList

      public static final java.util.List<java.lang.String> splitIntoList​(java.lang.String str, char splitChar)
    • split

      public static final java.lang.String[] split​(java.lang.String str, char splitChar)
    • vectorToStringArray

      public static final java.lang.String[] vectorToStringArray​(java.util.Vector v)
    • getNLengthString

      public static final java.lang.String getNLengthString​(int width, char fillChar)
    • getGMTDateStr

      public static final java.lang.String getGMTDateStr​(java.util.Date dt)
    • main

      public static final void main​(java.lang.String[] argv)
    • fillLeft

      public static final java.lang.String fillLeft​(java.lang.String str, int width, char fillChar)
    • fillRight

      public static final java.lang.String fillRight​(java.lang.String str, int width, char fillChar)
    • areStringsIdentical

      public static final boolean areStringsIdentical​(java.lang.String s1, java.lang.String s2)
    • fieldIndex

      public static final java.lang.String fieldIndex​(java.lang.String str, char delimiter, int n)
    • countFields

      public static final int countFields​(java.lang.String str, char delimiter)
    • sortStringArray

      public static final void sortStringArray​(java.lang.String[] array)
    • stripNonNumbers

      public static final java.lang.String stripNonNumbers​(java.lang.String str, char decimalChar)
    • isInteger

      public static final boolean isInteger​(java.lang.String str)
    • isAllNumber

      public static final boolean isAllNumber​(java.lang.String str)
    • isNumeric

      public static final boolean isNumeric​(java.lang.String str)
    • isDate

      public static final boolean isDate​(java.lang.String str)
    • guessDecimalType

      public static final char guessDecimalType​(java.lang.String currencyString)
    • decodeURL

      public static final java.lang.String decodeURL​(java.lang.String str)
      This method decodes a URL parameter that has special characters %encoded
    • decode

      public static final java.lang.String decode​(java.lang.String str)
    • encode

      public static final java.lang.String encode​(java.lang.String str)
    • convertToFileName

      public static final java.lang.String convertToFileName​(java.lang.String str)
      Performs a conversion from the given string to an encoded version that can be used as a file/URL name. This is new as of Moneydance build 325.
    • formatRate

      public static final java.lang.String formatRate​(double rate, char decimalChar)
    • formatShortRate

      public static final java.lang.String formatShortRate​(double rate, char decimalChar)
    • parseFraction

      public static final double parseFraction​(java.lang.String fraction, char decimalChar)
      Parse a fraction from a string - ie "1/5". Returns 1 if there was an error in the format
    • parseRate

      public static final double parseRate​(java.lang.String rateStr, char decimalChar)
    • parseRate

      public static final double parseRate​(java.lang.String rateStr, double defaultRate, char decimalChar)
    • parseDouble

      public static final double parseDouble​(java.lang.String val, char decimalChar)
    • parseDouble

      public static final double parseDouble​(java.lang.String val, double defaultVal, char decimalChar)
    • parseDoubleWithException

      public static final double parseDoubleWithException​(java.lang.String val, char decimalChar) throws java.lang.Exception
      Throws:
      java.lang.Exception
    • formatPercentage

      public static java.lang.String formatPercentage​(double value, char decimalChar)
      Formats the given double [0..1.00] as a percentage with either zero (if an even percentage) or two decimal digits showing.
      Parameters:
      value - The value as a decimal (is multiplied by 100 for display).
      decimalChar - The decimal character to display.
      Returns:
      A percentage for display to the user, which will have 2 decimal digits unless it is an even integer percent, in which case it will have no decimal digits displayed.
    • formatPercentage

      public static java.lang.String formatPercentage​(double value, char decimalChar, boolean forceDecimal)
      Formats the given double [0..1.00] as a percentage with either zero (if an even percentage) or two decimal digits showing. If forceDecimal is true, digits after the decimal point will be displayed regardless of whether it is an even percentage or not.
      Parameters:
      value - The value as a decimal (is multiplied by 100 for display).
      decimalChar - The decimal character to display.
      forceDecimal - True to always display digits after the decimal point, false to only display them if it is not an even integer percentage.
      Returns:
      A percentage for display to the user, which will have 2 decimal digits unless it is an even integer percent, in which case it will have no decimal digits displayed.
    • formatFixedDecimals

      public static java.lang.String formatFixedDecimals​(java.lang.String value, char decimalChar, int numDecimals)
      Formats the given double [0..1.00] as a percentage with the specified number of decimal digits showing.
      Parameters:
      value - The numeric string as currently formatted.
      decimalChar - The decimal character to display.
      numDecimals - The number of decimals to force display of.
      Returns:
      The provided string with exactly the number of decimal places requested.
    • formatMinDecimals

      public static java.lang.String formatMinDecimals​(java.lang.String value, char decimalChar, int numDecimals)
      Formats the given double [0..1.00] as a percentage with the specified number of decimal digits showing as a minimum, there could be more.
      Parameters:
      value - The numeric string as currently formatted.
      decimalChar - The decimal character to display.
      numDecimals - The number of decimals to force display of.
      Returns:
      The provided string with the number of decimal places requested as a minimum.
    • parseQuickenDate

      public static final long parseQuickenDate​(java.lang.String date)
    • looksLikeFormula

      public static final boolean looksLikeFormula​(java.lang.String expr)
    • parseFormula

      public static final double parseFormula​(java.lang.String str, char dec) throws java.lang.Exception
      Throws:
      java.lang.Exception
    • doNothing

      public static void doNothing()
    • replaceAll

      public static java.lang.String replaceAll​(java.lang.String str, java.lang.String toReplace, java.lang.String replaceWith)
      Replace one or more tokens inside another string with a value.
      Parameters:
      str - The string containing the token(s).
      toReplace - The token, case sensitive, to be replaced.
      replaceWith - The string to replace the token with. Can be null.
      Returns:
      The str string with the token replaced with a new value.
    • isEmpty

      public static boolean isEmpty​(java.lang.String candidate)
      Null safe check for "" (empty string).
      Parameters:
      candidate - the String to evaluate.
      Returns:
      true if candidate is null or "" (empty string)
    • isBlank

      public static boolean isBlank​(java.lang.String candidate)
      Null safe check for a String with nothing but whitespace characters.
      Parameters:
      candidate - the String to evaluate.
      Returns:
      true if the candidate is null or all whitespace.
    • stripExtension

      public static java.lang.String stripExtension​(java.lang.String fileName)
      Returns the filename passed as argument without extension
    • getExtensionFromFilename

      public static java.lang.String getExtensionFromFilename​(java.lang.String fileName)
      Returns the extension, if any, on the given file name. Returns an empty string if there was no extension and null if the given filename was null.
    • stripNonFilenameSafeCharacters

      public static java.lang.String stripNonFilenameSafeCharacters​(java.lang.String filename)
      Strip all characters that should not appear in a filename (including path characters)
    • stripPath

      public static java.lang.String stripPath​(java.lang.String filename)
      Returns the file name with any path stripped from the beginning. This is available as of build 812
      Since:
      build 812
    • getNonNull

      public static java.lang.String getNonNull​(java.lang.String str)
      Convenience method that Returns the given string or the empty string if the given string is null
      Since:
      build 1526
    • capitalizeFirst

      public static java.lang.String capitalizeFirst​(java.lang.String string)
      Return a String with the first letter capitalized.
      Parameters:
      string -
      Returns:
    • computeLevenshteinDistance

      public static int computeLevenshteinDistance​(java.lang.CharSequence str1, java.lang.CharSequence str2)
    • parseURLParameters

      public static void parseURLParameters​(java.lang.String paramStr, java.util.Map<java.lang.String,​java.lang.String> map)
    • stripExtraCharacters

      public static java.lang.String stripExtraCharacters​(java.lang.String str)
      Remove leading and trailing quotes or white space from a string
    • join

      public static java.lang.String join​(java.util.List<java.lang.String> strings, java.lang.String joinString)
      Concatenate all strings in the given list with the joinString between them
      Since:
      Moneydance 2021