Class Strings


  • public class Strings
    extends Object
    Created by tchemit on 29/12/2017.
    Author:
    Tony Chemit - dev@tchemit.fr
    • Constructor Detail

      • Strings

        public Strings()
    • Method Detail

      • encodeSHA1

        public static String encodeSHA1​(String toEncode)
        Convert a String to SHA1.
        Parameters:
        toEncode - string to encode
        Returns:
        sha1 corresponding
        Throws:
        IllegalStateException - if could not found algorithm SHA1
      • convertTime

        public static String convertTime​(long value)
        Converts an time delay into a human readable format.
        Parameters:
        value - the delay to convert
        Returns:
        the memory representation of the given value
        See Also:
        convert(long, double[], String[])
      • convertTime

        public static String convertTime​(long value,
                                         long value2)
        Converts an time period into a human readable format.
        Parameters:
        value - the begin time
        value2 - the end time
        Returns:
        the time representation of the given value
        See Also:
        convert(long, double[], String[])
      • convertMemory

        public static String convertMemory​(long value)
        Converts an memory measure into a human readable format.
        Parameters:
        value - the memory measure to convert
        Returns:
        the memory representation of the given value
        See Also:
        convert(long, double[], String[])
      • convert

        public static String convert​(long value,
                                     double[] factors,
                                     String[] unites)
        Note: this method use the current locale (the Locale.getDefault()) in the method MessageFormat(String).
        Parameters:
        value - value to convert
        factors - factors used form conversion
        unites - libelle of unites to use
        Returns:
        the converted representation of the given value
      • convertToConstantName

        public static String convertToConstantName​(String name)
        Convertir un nom en une constante Java

        Les seuls caractères autorisés sont les alpha numériques, ains que l'underscore. tous les autres caractères seront ignorés.

        Parameters:
        name - le nom à convertir
        Returns:
        la constante générée
      • getRelativeCamelCaseName

        public static String getRelativeCamelCaseName​(String packageName,
                                                      String fullyQualifiedName,
                                                      String... excludes)
        Get the relative camel case name for the given fullyQualifiedName.

        first try to remove packageName, then any excludes on starts, at last reduce with came case.

        Parameters:
        packageName - starts package
        fullyQualifiedName - fqn to transform
        excludes - optional starts packages to remove
        Returns:
        the camel case result
      • leftPad

        public static String leftPad​(String str,
                                     int size,
                                     String padStr)

        Left pad a String with a specified String.

        Pad to a size of size.

         Strings.leftPad(null, *, *)      = null
         Strings.leftPad("", 3, "z")      = "zzz"
         Strings.leftPad("bat", 3, "yz")  = "bat"
         Strings.leftPad("bat", 5, "yz")  = "yzbat"
         Strings.leftPad("bat", 8, "yz")  = "yzyzybat"
         Strings.leftPad("bat", 1, "yz")  = "bat"
         Strings.leftPad("bat", -1, "yz") = "bat"
         Strings.leftPad("bat", 5, null)  = "  bat"
         Strings.leftPad("bat", 5, "")    = "  bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padStr - the String to pad with, null or empty treated as single space
        Returns:
        left padded String or original String if no padding is necessary, null if null String input
      • leftPad

        public static String leftPad​(String str,
                                     int size)

        Left pad a String with spaces (' ').

        The String is padded to the size of size.

         Strings.leftPad(null, *)   = null
         Strings.leftPad("", 3)     = "   "
         Strings.leftPad("bat", 3)  = "bat"
         Strings.leftPad("bat", 5)  = "  bat"
         Strings.leftPad("bat", 1)  = "bat"
         Strings.leftPad("bat", -1) = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        Returns:
        left padded String or original String if no padding is necessary, null if null String input
      • leftPad

        public static String leftPad​(String str,
                                     int size,
                                     char padChar)

        Left pad a String with a specified character.

        Pad to a size of size.

         Strings.leftPad(null, *, *)     = null
         Strings.leftPad("", 3, 'z')     = "zzz"
         Strings.leftPad("bat", 3, 'z')  = "bat"
         Strings.leftPad("bat", 5, 'z')  = "zzbat"
         Strings.leftPad("bat", 1, 'z')  = "bat"
         Strings.leftPad("bat", -1, 'z') = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padChar - the character to pad with
        Returns:
        left padded String or original String if no padding is necessary, null if null String input
      • isEmpty

        public static boolean isEmpty​(String o)
      • isNotEmpty

        public static boolean isNotEmpty​(String o)
      • isNumeric

        public static boolean isNumeric​(String cs)

        Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.

        null will return false. An empty CharSequence (length()=0) will return false.

        Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.

         Strings.isNumeric(null)   = false
         Strings.isNumeric("")     = false
         Strings.isNumeric("  ")   = false
         Strings.isNumeric("123")  = true
         Strings.isNumeric("१२३")  = true
         Strings.isNumeric("12 3") = false
         Strings.isNumeric("ab2c") = false
         Strings.isNumeric("12-3") = false
         Strings.isNumeric("12.3") = false
         Strings.isNumeric("-123") = false
         Strings.isNumeric("+123") = false
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if only contains digits, and is non-null
      • capitalize

        public static String capitalize​(String str)

        Capitalizes a String changing the first character to title case as per Character.toTitleCase(int). No other characters are changed.

         Strings.capitalize(null)  = null
         Strings.capitalize("")    = ""
         Strings.capitalize("cat") = "Cat"
         Strings.capitalize("cAt") = "CAt"
         Strings.capitalize("'cat'") = "'cat'"
         
        Parameters:
        str - the String to capitalize, may be null
        Returns:
        the capitalized String, null if null String input
      • removeEnd

        public static String removeEnd​(String str,
                                       String remove)

        Removes a substring only if it is at the end of a source string, otherwise returns the source string.

        A null source string will return null. An empty ("") source string will return the empty string. A null search string will return the source string.

         Strings.removeEnd(null, *)      = null
         Strings.removeEnd("", *)        = ""
         Strings.removeEnd(*, null)      = *
         Strings.removeEnd("www.domain.com", ".com.")  = "www.domain.com"
         Strings.removeEnd("www.domain.com", ".com")   = "www.domain"
         Strings.removeEnd("www.domain.com", "domain") = "www.domain.com"
         Strings.removeEnd("abc", "")    = "abc"
         
        Parameters:
        str - the source String to search, may be null
        remove - the String to search for and remove, may be null
        Returns:
        the substring with the string removed if found, null if null String input
        Since:
        2.1
      • rightPad

        public static String rightPad​(String str,
                                      int size,
                                      String padStr)

        Right pad a String with a specified String.

        The String is padded to the size of size.

         Strings.rightPad(null, *, *)      = null
         Strings.rightPad("", 3, "z")      = "zzz"
         Strings.rightPad("bat", 3, "yz")  = "bat"
         Strings.rightPad("bat", 5, "yz")  = "batyz"
         Strings.rightPad("bat", 8, "yz")  = "batyzyzy"
         Strings.rightPad("bat", 1, "yz")  = "bat"
         Strings.rightPad("bat", -1, "yz") = "bat"
         Strings.rightPad("bat", 5, null)  = "bat  "
         Strings.rightPad("bat", 5, "")    = "bat  "
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padStr - the String to pad with, null or empty treated as single space
        Returns:
        right padded String or original String if no padding is necessary, null if null String input
      • rightPad

        public static String rightPad​(String str,
                                      int size,
                                      char padChar)

        Right pad a String with a specified character.

        The String is padded to the size of size.

         Strings.rightPad(null, *, *)     = null
         Strings.rightPad("", 3, 'z')     = "zzz"
         Strings.rightPad("bat", 3, 'z')  = "bat"
         Strings.rightPad("bat", 5, 'z')  = "batzz"
         Strings.rightPad("bat", 1, 'z')  = "bat"
         Strings.rightPad("bat", -1, 'z') = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padChar - the character to pad with
        Returns:
        right padded String or original String if no padding is necessary, null if null String input
      • substringAfter

        public static String substringAfter​(String str,
                                            String separator)

        Gets the substring after the first occurrence of a separator. The separator is not returned.

        A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the empty string if the input string is not null.

        If nothing is found, the empty string is returned.

         Strings.substringAfter(null, *)      = null
         Strings.substringAfter("", *)        = ""
         Strings.substringAfter(*, null)      = ""
         Strings.substringAfter("abc", "a")   = "bc"
         Strings.substringAfter("abcba", "b") = "cba"
         Strings.substringAfter("abc", "c")   = ""
         Strings.substringAfter("abc", "d")   = ""
         Strings.substringAfter("abc", "")    = "abc"
         
        Parameters:
        str - the String to get a substring from, may be null
        separator - the String to search for, may be null
        Returns:
        the substring after the first occurrence of the separator, null if null String input
      • substringBefore

        public static String substringBefore​(String str,
                                             String separator)

        Gets the substring before the first occurrence of a separator. The separator is not returned.

        A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the input string.

        If nothing is found, the string input is returned.

         Strings.substringBefore(null, *)      = null
         Strings.substringBefore("", *)        = ""
         Strings.substringBefore("abc", "a")   = ""
         Strings.substringBefore("abcba", "b") = "a"
         Strings.substringBefore("abc", "c")   = "ab"
         Strings.substringBefore("abc", "d")   = "abc"
         Strings.substringBefore("abc", "")    = ""
         Strings.substringBefore("abc", null)  = "abc"
         
        Parameters:
        str - the String to get a substring from, may be null
        separator - the String to search for, may be null
        Returns:
        the substring before the first occurrence of the separator, null if null String input
      • split

        public static String[] split​(String str,
                                     String separatorChars)

        Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.

        The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

        A null input String returns null. A null separatorChars splits on whitespace.

         Strings.split(null, *)         = null
         Strings.split("", *)           = []
         Strings.split("abc def", null) = ["abc", "def"]
         Strings.split("abc def", " ")  = ["abc", "def"]
         Strings.split("abc  def", " ") = ["abc", "def"]
         Strings.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
         
        Parameters:
        str - the String to parse, may be null
        separatorChars - the characters used as the delimiters, null splits on whitespace
        Returns:
        an array of parsed Strings, null if null String input
      • repeat

        public static String repeat​(char ch,
                                    int repeat)

        Returns padding using the specified delimiter repeated to a given length.

         Strings.repeat('e', 0)  = ""
         Strings.repeat('e', 3)  = "eee"
         Strings.repeat('e', -2) = ""
         
        Parameters:
        ch - character to repeat
        repeat - number of times to repeat char, negative treated as zero
        Returns:
        String with repeated character