Class TextUtil


  • public final class TextUtil
    extends Object
    This class permits to manipulate texts.
    Version:
    17.0 2020-01-04 14:41:39
    Author:
    Stéphane GALLAND
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    text
    • Method Detail

      • equalsIgnoreCase

        @Pure
        public static boolean equalsIgnoreCase​(String firstText,
                                               String secondText,
                                               boolean isNullEmptyEquivalence)
        Enforced version of the equality test on two strings with case ignoring. This enforced version supported null values given as parameters.
        Parameters:
        firstText - first text.
        secondText - second text.
        isNullEmptyEquivalence - indicates if the null value is assimilated to the empty string.
        Returns:
        true if a is equal to b; otherwise false.
      • encodeBase26

        @Pure
        public static String encodeBase26​(int number)
        Replies a base 26 encoding string for the given number.
        Parameters:
        number - the number to encode.
        Returns:
        the base 26 encoding.
        Since:
        4.0
      • getHtmlToJavaTranslationTable

        @Pure
        public static Map<String,​Integer> getHtmlToJavaTranslationTable()
        Replies the html-to-java's translation table.

        This method read the translations from the resource file HTML_TRANS_TBL.

        Returns:
        the translation table or null if none was found. The translation table maps an HTML entity to its corresponding ISO chararacter code.
        Since:
        4.0
      • getJavaToHTMLTranslationTable

        @Pure
        public static Map<Character,​String> getJavaToHTMLTranslationTable()
        Replies the java-to-html's translation table.

        This method read the translations from the resource file HTML_TRANS_TBL.

        Returns:
        the translation table or null if none was found. The translation table maps an ISO character code to its corresponding HTML entity.
        Since:
        4.0
      • parseHTML

        @Pure
        public static String parseHTML​(String html)
        Parse the given HTML text and replace all the HTML entities by the corresponding unicode character.
        Parameters:
        html - is the HTML to convert.
        Returns:
        the unicode representation of the given html text.
        Since:
        4.0
        See Also:
        toHTML(String)
      • toHTML

        @Pure
        public static String toHTML​(String text)
        Translate all the special character from the given text to their equivalent HTML entities.
        Parameters:
        text - is the text to convert.
        Returns:
        the HTML text which is corresponding to the given text.
        Since:
        4.0
        See Also:
        parseHTML(String)
      • cutString

        @Pure
        public static String cutString​(String text,
                                       int column)
        Format the text to be sure that each line is not more longer than the specified quantity of characters.
        Parameters:
        text - is the string to cut
        column - is the column number that corresponds to the splitting point.
        Returns:
        the given text splitted in lines separated by \n.
      • cutStringAsArray

        @Pure
        public static String[] cutStringAsArray​(String text,
                                                int column)
        Format the text to be sure that each line is not more longer than the specified quantity of characters.
        Parameters:
        text - is the string to cut
        column - is the column number that corresponds to the splitting point.
        Returns:
        the given text splitted in lines separated by \n.
      • cutStringAsArray

        public static void cutStringAsArray​(String text,
                                            TextUtil.CutStringCritera critera,
                                            List<String> output)
        Format the text to be sure that each line is not more longer than the specified critera.
        Parameters:
        text - is the string to cut
        critera - is the critera to respect.
        output - is the given text splitted in lines separated by \n.
        Since:
        4.0
      • getMnemonicChar

        @Pure
        public static char getMnemonicChar​(String text)
        Replies the character which follow the first '&'.
        Parameters:
        text - is the text to scan.
        Returns:
        the character that is following the first '&' or '\0'
      • removeMnemonicChar

        @Pure
        public static String removeMnemonicChar​(String text)
        Remove the mnemonic char from the specified string.
        Parameters:
        text - is the text to scan.
        Returns:
        the given text without the mnemonic character.
      • getAccentTranslationTable

        @Pure
        public static Map<Character,​String> getAccentTranslationTable()
        Replies the accent's translation table.

        This method read the translations from the resource file ACCENT_TRANS_TBL.

        Returns:
        the translation table or null if none was found.
      • removeAccents

        @Pure
        public static String removeAccents​(String text)
        Remove the accents inside the specified string.
        Parameters:
        text - is the string into which the accents must be removed.
        Returns:
        the given string without the accents
      • removeAccents

        @Pure
        public static String removeAccents​(String text,
                                           Map<Character,​String> map)
        Remove the accents inside the specified string.
        Parameters:
        text - is the string into which the accents must be removed.
        map - is the translation table from an accentuated character to an unaccentuated character.
        Returns:
        the given string without the accents
      • splitBrackets

        @Pure
        public static String[] splitBrackets​(String str)
        Split the given string according to brackets. The brackets are used to delimit the groups of characters.

        Examples:

        • splitBrackets("{a}{b}{cd}") returns the array ["a","b","cd"]
        • splitBrackets("abcd") returns the array ["abcd"]
        • splitBrackets("a{bcd") returns the array ["a","bcd"]
        Parameters:
        str - is the strig with brackets.
        Returns:
        the groups of strings
      • split

        @Pure
        public static String[] split​(char leftSeparator,
                                     char rightSeparator,
                                     String str)
        Split the given string according to the separators. The separators are used to delimit the groups of characters.

        Examples:

        • split('{','}',"{a}{b}{cd}") returns the array ["a","b","cd"]
        • split('{','}',"abcd") returns the array ["abcd"]
        • split('{','}',"a{bcd") returns the array ["a","bcd"]
        Parameters:
        leftSeparator - is the left separator.
        rightSeparator - is the right separator.
        str - is the strig with brackets.
        Returns:
        the groups of strings
        Since:
        4.0
      • splitBracketsAsList

        @Pure
        public static List<String> splitBracketsAsList​(String str)
        Split the given string according to brackets. The brackets are used to delimit the groups of characters.

        Examples:

        • splitBrackets("{a}{b}{cd}") returns the array ["a","b","cd"]
        • splitBrackets("abcd") returns the array ["abcd"]
        • splitBrackets("a{bcd") returns the array ["a","bcd"]
        Parameters:
        str - is the elements enclosed by backets.
        Returns:
        the groups of strings
      • splitAsList

        @Pure
        public static List<String> splitAsList​(char leftSeparator,
                                               char rightSeparator,
                                               String str)
        Split the given string according to separators. The separators are used to delimit the groups of characters.

        Examples:

        • split('{','}',"{a}{b}{cd}") returns the array ["a","b","cd"]
        • split('{','}',"abcd") returns the array ["abcd"]
        • split('{','}',"a{bcd") returns the array ["a","bcd"]
        Parameters:
        leftSeparator - is the left separator.
        rightSeparator - is the right separator.
        str - is the elements enclosed by backets.
        Returns:
        the groups of strings
      • splitBracketsAsUUIDs

        @Pure
        public static List<UUID> splitBracketsAsUUIDs​(String str)
        Split the given string according to brackets. The brackets are used to delimit the groups of characters.

        Examples:

        • splitBrackets("{a}{b}{cd}") returns the array ["a","b","cd"]
        • splitBrackets("abcd") returns the array ["abcd"]
        • splitBrackets("a{bcd") returns the array ["a","bcd"]
        Parameters:
        str - is the elements enclosed by backets.
        Returns:
        the groups of strings
      • splitAsUUIDs

        @Pure
        public static List<UUID> splitAsUUIDs​(char leftSeparator,
                                              char rightSeparator,
                                              String str)
        Split the given string according to separators. The separators are used to delimit the groups of characters.

        Examples:

        • split('{','}',"{a}{b}{cd}") returns the array ["a","b","cd"]
        • split('{','}',"abcd") returns the array ["abcd"]
        • split('{','}',"a{bcd") returns the array ["a","bcd"]
        Parameters:
        leftSeparator - is the left separator.
        rightSeparator - is the right separator.
        str - is the elements enclosed by backets.
        Returns:
        the groups of strings
        Since:
        4.0
      • mergeBrackets

        @Pure
        public static <T> String mergeBrackets​(T... strs)
        Merge the given strings with to brackets. The brackets are used to delimit the groups of characters.

        Examples:

        • mergeBrackets("a","b","cd") returns the string "{a}{b}{cd}"
        • mergeBrackets("a{bcd") returns the string "{a{bcd}"
        Type Parameters:
        T - is the type of the parameters.
        Parameters:
        strs - is the array of strings.
        Returns:
        the string with bracketed strings.
      • mergeBrackets

        @Pure
        public static String mergeBrackets​(Iterable<?> strs)
        Merge the given strings with to brackets. The brackets are used to delimit the groups of characters.

        Examples:

        • mergeBrackets("a","b","cd") returns the string "{a}{b}{cd}"
        • mergeBrackets("a{bcd") returns the string "{a{bcd}"
        Parameters:
        strs - is the array of strings.
        Returns:
        the string with bracketed strings.
        See Also:
        join(char, char, Iterable)
      • join

        @Pure
        public static <T> String join​(char leftSeparator,
                                      char rightSeparator,
                                      T... strs)
        Merge the given strings with to separators. The separators are used to delimit the groups of characters.

        Examples:

        • merge('{','}',"a","b","cd") returns the string "{a}{b}{cd}"
        • merge('{','}',"a{bcd") returns the string "{a{bcd}"
        Type Parameters:
        T - is the type of the parameters.
        Parameters:
        leftSeparator - is the left separator to use.
        rightSeparator - is the right separator to use.
        strs - is the array of strings.
        Returns:
        the string with merged strings.
        Since:
        4.0
      • join

        @Pure
        public static String join​(char leftSeparator,
                                  char rightSeparator,
                                  Iterable<?> strs)
        Merge the given strings with to separators. The separators are used to delimit the groups of characters.

        Examples:

        • merge('{','}',"a","b","cd") returns the string "{a}{b}{cd}"
        • merge('{','}',"a{bcd") returns the string "{a{bcd}"
        Parameters:
        leftSeparator - is the left separator to use.
        rightSeparator - is the right separator to use.
        strs - is the array of strings.
        Returns:
        the string with merged strings.
        Since:
        4.0
      • join

        @Pure
        public static <T> String join​(String joinText,
                                      T... elements)
        Join the elements of the given array with the given join text.
        Type Parameters:
        T - is the type of the elements
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  boolean... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  byte... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  char... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  short... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  int... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  long... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  float... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  double... elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  Iterable<?> elements)
        Join the elements of the given array with the given join text.
        Parameters:
        joinText - the text to use for joining.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static <T> String join​(String joinText,
                                      String prefix,
                                      String postfix,
                                      T... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Type Parameters:
        T - is the type of the elements
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  Iterable<?> elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  boolean... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  byte... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  char... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  short... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  int... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  long... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  float... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • join

        @Pure
        public static String join​(String joinText,
                                  String prefix,
                                  String postfix,
                                  double... elements)
        Join the elements of the given array with the given join text. The prefix and postfix values will be put just before and just after each element respectively.
        Parameters:
        joinText - the text to use for joining.
        prefix - the text to put as prefix.
        postfix - the text to put as postfix.
        elements - the parts of text to join.
        Returns:
        the joining text
      • equalsIgnoreAccents

        @Pure
        public static boolean equalsIgnoreAccents​(String s1,
                                                  String s2,
                                                  Map<Character,​String> map)
        Compares this String to another String, ignoring accent considerations. Two strings are considered equal ignoring accents if they are of the same length, and corresponding characters in the two strings are equal ignoring accents.

        This method is equivalent to:

        
         TextUtil.removeAccents(s1,map).equals(TextUtil.removeAccents(s2,map));
         
        Parameters:
        s1 - is the first string to compare.
        s2 - is the second string to compare.
        map - is the translation table from an accentuated character to an unaccentuated character.
        Returns:
        true if the argument is not null and the Strings are equal, ignoring case; false otherwise.
        See Also:
        removeAccents(String, Map)
      • equalsIgnoreCaseAccents

        @Pure
        public static boolean equalsIgnoreCaseAccents​(String s1,
                                                      String s2,
                                                      Map<Character,​String> map)
        Compares this String to another String, ignoring case and accent considerations. Two strings are considered equal ignoring case and accents if they are of the same length, and corresponding characters in the two strings are equal ignoring case and accents.

        This method is equivalent to:

        
         TextUtil.removeAccents(s1,map).equalsIgnoreCase(TextUtil.removeAccents(s2,map));
         
        Parameters:
        s1 - is the first string to compare.
        s2 - is the second string to compare.
        map - is the translation table from an accentuated character to an unaccentuated character.
        Returns:
        true if the argument is not null and the Strings are equal, ignoring case; false otherwise.
        See Also:
        removeAccents(String, Map)
      • toLowerCaseWithoutAccent

        @Pure
        public static String toLowerCaseWithoutAccent​(String text)
        Translate the specified string to lower case and remove the accents.
        Parameters:
        text - is the text to scan.
        Returns:
        the given string without the accents and lower cased
      • toLowerCaseWithoutAccent

        @Pure
        public static String toLowerCaseWithoutAccent​(String text,
                                                      Map<Character,​String> map)
        Translate the specified string to lower case and remove the accents.
        Parameters:
        text - is the text to scan.
        map - is the translation table from an accentuated character to an unaccentuated character.
        Returns:
        the given string without the accents and lower cased
      • toUpperCaseWithoutAccent

        @Pure
        public static String toUpperCaseWithoutAccent​(String text)
        Translate the specified string to upper case and remove the accents.
        Parameters:
        text - is the text to scan.
        Returns:
        the given string without the accents and upper cased
      • toUpperCaseWithoutAccent

        @Pure
        public static String toUpperCaseWithoutAccent​(String text,
                                                      Map<Character,​String> map)
        Translate the specified string to upper case and remove the accents.
        Parameters:
        text - is the text to scan.
        map - is the translation table from an accentuated character to an unaccentuated character.
        Returns:
        the given string without the accents and upper cased
      • formatTime

        @Pure
        public static String formatTime​(double amount,
                                        TimeUnit unit)
        Compute the better metric representing the given time amount and reply a string representation of the given amount with this selected unit.

        This function try to use a greater metric unit.

        Parameters:
        amount - is the amount expressed in the given unit.
        unit - is the unit of the given amount.
        Returns:
        a string representation of the given amount.
      • formatHex

        @Pure
        public static String formatHex​(int amount,
                                       int digits)
        Format the given int value to hexadecimal.
        Parameters:
        amount - the value to convert.
        digits - the minimal number of digits.
        Returns:
        a string representation of the given value.
        Since:
        15.0
      • formatDouble

        @Pure
        public static String formatDouble​(double amount,
                                          int decimalCount)
        Format the given double value.
        Parameters:
        amount - the value to convert.
        decimalCount - is the maximal count of decimal to put in the string.
        Returns:
        a string representation of the given value.
      • formatFloat

        @Pure
        public static String formatFloat​(float amount,
                                         int decimalCount)
        Format the given float value.
        Parameters:
        amount - the value to convert.
        decimalCount - is the maximal count of decimal to put in the string.
        Returns:
        a string representation of the given value.
      • getLevenshteinDistance

        public static int getLevenshteinDistance​(String firstString,
                                                 String secondString)
        Compute the Levenshstein distance between two strings.

        Null string is assimilated to the empty string.

        Parameters:
        firstString - first string.
        secondString - second string.
        Returns:
        the Levenshstein distance.
        See Also:
        "https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance"
      • toJavaString

        @Pure
        public static String toJavaString​(String text)
        Translate the given string to its Java string equivalent. All the special characters will be escaped. The enclosing double quote characters are not added.

        The Java special characters are listed within StringEscaper.JAVA_SPECIAL_CHARS. Any character outside the range [StringEscaper.JAVA_MIN_CHAR, StringEscaper.JAVA_MAX_CHAR] is automatically escaped.

        Parameters:
        text - is the text to convert.
        Returns:
        the Java string for the text.
        Since:
        15.0
      • toJsonString

        @Pure
        public static String toJsonString​(String text)
        Translate the given string to its Json string equivalent. All the special characters will be escaped. The enclosing double quote characters are not added.

        The Json escaping extends the Java escaping by protecting the / character.

        Parameters:
        text - is the text to convert.
        Returns:
        the Json string for the text.
        Since:
        15.0