Class Strings


  • public final class Strings
    extends java.lang.Object
    This class contains utils methods to work with strings.
    Author:
    javaito
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Strings.Builder
      This class is a StringBuilder wrapper that add a way to append objects with a buffer to be used in the next append or discard the buffer is there are not a other append operation
      static class  Strings.StandardOutput  
      static class  Strings.TaggedMessages  
    • Constructor Summary

      Constructors 
      Constructor Description
      Strings()  
    • Method Summary

      Modifier and Type Method Description
      static java.util.Set<java.lang.Integer> allIndexOf​(java.lang.String value, java.lang.String foundedValue)
      Return all the index into the string value where found the specific founded value.
      static java.util.Set<java.lang.Integer> allIndexOf​(java.lang.String value, java.lang.String foundedValue, boolean desc)
      Return all the index into the string value where found the specific founded value.
      static java.lang.String bytesToHex​(byte[] bytes)
      Returns a hexadecimal representation of the byte array.
      static java.lang.String capitalize​(java.lang.String value)
      Replace the first character for his upper case representation.
      static java.lang.String createTaggedMessage​(java.lang.String message, java.lang.String... tags)
      This method create a tagged message with the format $@{tag,tag,tag...}message
      static java.util.UUID createUUIDFromStringHash​(java.lang.String value)
      This method creates a uuid from a hash code of the string object.
      static java.util.UUID createUUIDFromStringHash​(java.lang.String value1, java.lang.String value2)
      This method creates a uuid from a hash code of the string object.
      static java.lang.Object deductInstance​(java.lang.String value)
      Returns the instance of the class that is deducted from the string value.
      static java.lang.String getGroupIndex​(java.lang.String value, java.lang.String groupIndicator)
      Return the first replaceable index founded.
      static java.lang.String getNearFrom​(java.lang.String value, int position, int length)
      Returns a shorted string that the original centered into the position parameter and with
      static int getNoMatchPlace​(java.util.regex.Matcher matcher, java.lang.String value)
      Returns the place where the regex is no matching with the value.
      static java.util.Map<java.lang.String,​java.lang.String> getTagsFromMessage​(java.lang.String taggedMessage)
      This method create a hash map indexing the same message with the different tags.
      static java.util.List<java.lang.String> group​(java.lang.String value)
      Return the list with all the groups and sub groups of the value.
      static java.util.List<java.lang.String> groupRichText​(java.lang.String value)
      Returns all the rich texts contained into the value and the value (in the last place) with all the replaceable places for each rich text.
      static byte[] hexToBytes​(java.lang.String hex)
      Returns a byte array based on the hexadecimal representation.
      static java.lang.String join​(java.util.Collection<java.lang.String> values, java.lang.String separator)
      Return the string that result of join all the values separated by the separated value specified.
      static java.lang.String join​(java.util.Collection<java.lang.String> values, java.lang.String startValue, java.lang.String endValue, java.lang.String separator)
      Return the string that result of join all the values separated by the separated value specified and wrapped by the start and and value.
      static java.lang.String join​(java.util.stream.Stream<java.lang.String> values, java.lang.String separator)
      Return the string that result of join all the values separated by the separated value specified.
      static java.lang.String join​(java.util.stream.Stream<java.lang.String> values, java.lang.String startValue, java.lang.String endValue, java.lang.String separator)
      Return the string that result of join all the values separated by the separated value specified and wrapped by the start and and value.
      static java.lang.String joinWords​(java.lang.String value, java.lang.String separator)
      This method join the words into the value identifying each of word for the uppercase character.
      static java.lang.String leftPad​(java.lang.String value, int paddingSize)
      Complete the left side of the value with n instance of the default padding value (' ').
      static java.lang.String leftPad​(java.lang.String value, java.lang.String paddingValue, int paddingSize)
      Complete the left side of the value with n instance of the padding value.
      static java.lang.String removeLines​(java.lang.String value)
      This method replace the combination of character \r\n and the character \n for white space character.
      static java.util.List<java.lang.String> replaceableGroup​(java.lang.String value)
      Return a list with all groups and sub groups in ascendant order with replacement places that refer some index into the same list.
      static java.lang.String reverseGrouping​(java.lang.String value, java.util.List<java.lang.String> groups)
      Reverts the grouping action over the specific value.
      static java.lang.String reverseRichTextGrouping​(java.lang.String value, java.util.List<java.lang.String> richTextGroups)
      Reverts the grouping action over the specific value.
      static java.lang.String rightPad​(java.lang.String value, int paddingSize)
      Complete the right side of the value with n instance of the default padding value (' ').
      static java.lang.String rightPad​(java.lang.String value, java.lang.String paddingValue, int paddingSize)
      Complete the right side of the value with n instance of the padding value.
      static java.lang.String[] splitByLength​(java.lang.String value, int length)
      This method splits the string value in n substring with the same length except for the last substring that could be smaller than the rest of the substrings.
      static java.lang.String splitInWord​(java.lang.String value, java.lang.String separator)
      This method search all the upper case characters into the value parameter and separates the value using this upper case characters like limit adding between words the separator value.
      static java.lang.String trim​(java.lang.String value, java.lang.String limitStrings)
      This method trim the first and the last value if this values are equals that the parameter.
      static java.lang.String trim​(java.lang.String value, java.lang.String startingString, java.lang.String endingString)
      This method trim the first and the last value if this values are equals that the parameters.
      static java.lang.String uncapitalize​(java.lang.String value)
      Replace the first character for his lower case representation.
      • Methods inherited from class java.lang.Object

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

      • Strings

        public Strings()
    • Method Detail

      • trim

        public static java.lang.String trim​(java.lang.String value,
                                            java.lang.String limitStrings)
        This method trim the first and the last value if this values are equals that the parameter.
        Parameters:
        value - Value to trim.
        limitStrings - Value to compare with the start and end of the value.
        Returns:
        Returns the value trimmed
      • trim

        public static java.lang.String trim​(java.lang.String value,
                                            java.lang.String startingString,
                                            java.lang.String endingString)
        This method trim the first and the last value if this values are equals that the parameters.
        Parameters:
        value - Value to trim.
        startingString - Value to compare with the start of the value.
        endingString - Value to compare with the end of the value.
        Returns:
        Returns the value trimmed.
      • removeLines

        public static java.lang.String removeLines​(java.lang.String value)
        This method replace the combination of character \r\n and the character \n for white space character.
        Parameters:
        value - String to remove the lines.
        Returns:
        String value without lines.
      • join

        public static java.lang.String join​(java.util.Collection<java.lang.String> values,
                                            java.lang.String separator)
        Return the string that result of join all the values separated by the separated value specified.
        Parameters:
        values - Values to join.
        separator - Separator value.
        Returns:
        Result of the join operation.
      • join

        public static java.lang.String join​(java.util.stream.Stream<java.lang.String> values,
                                            java.lang.String separator)
        Return the string that result of join all the values separated by the separated value specified.
        Parameters:
        values - Values to join.
        separator - Separator value.
        Returns:
        Result of the join operation.
      • join

        public static java.lang.String join​(java.util.Collection<java.lang.String> values,
                                            java.lang.String startValue,
                                            java.lang.String endValue,
                                            java.lang.String separator)
        Return the string that result of join all the values separated by the separated value specified and wrapped by the start and and value.
        Parameters:
        values - Values to join.
        startValue - Wrapped start value.
        endValue - Wrapped end value.
        separator - Separator value.
        Returns:
        Result of the join operation.
      • join

        public static java.lang.String join​(java.util.stream.Stream<java.lang.String> values,
                                            java.lang.String startValue,
                                            java.lang.String endValue,
                                            java.lang.String separator)
        Return the string that result of join all the values separated by the separated value specified and wrapped by the start and and value.
        Parameters:
        values - Values to join.
        startValue - Wrapped start value.
        endValue - Wrapped end value.
        separator - Separator value.
        Returns:
        Result of the join operation.
      • capitalize

        public static java.lang.String capitalize​(java.lang.String value)
        Replace the first character for his upper case representation.
        Parameters:
        value - Value to replace.
        Returns:
        Replaced value.
      • uncapitalize

        public static java.lang.String uncapitalize​(java.lang.String value)
        Replace the first character for his lower case representation.
        Parameters:
        value - Value to replace.
        Returns:
        Replaced value.
      • splitInWord

        public static java.lang.String splitInWord​(java.lang.String value,
                                                   java.lang.String separator)
        This method search all the upper case characters into the value parameter and separates the value using this upper case characters like limit adding between words the separator value.
        Parameters:
        value - Value to split.
        separator - Separator value.
        Returns:
        String with separated words.
      • joinWords

        public static java.lang.String joinWords​(java.lang.String value,
                                                 java.lang.String separator)
        This method join the words into the value identifying each of word for the uppercase character.
        Parameters:
        value - Value to join.
        separator - String to delimits the words.
        Returns:
        Joined value.
      • leftPad

        public static java.lang.String leftPad​(java.lang.String value,
                                               int paddingSize)
        Complete the left side of the value with n instance of the default padding value (' ').
        Parameters:
        value - Value to be completed.
        paddingSize - Number of instance to pad the value.
        Returns:
        New string with the left side padding.
      • leftPad

        public static java.lang.String leftPad​(java.lang.String value,
                                               java.lang.String paddingValue,
                                               int paddingSize)
        Complete the left side of the value with n instance of the padding value.
        Parameters:
        value - Value to be completed.
        paddingValue - Padding value.
        paddingSize - Number of instance to pad the value.
        Returns:
        New string with the left side padding.
      • rightPad

        public static java.lang.String rightPad​(java.lang.String value,
                                                int paddingSize)
        Complete the right side of the value with n instance of the default padding value (' ').
        Parameters:
        value - Value to be completed.
        paddingSize - Number of instance to pad the value.
        Returns:
        New string with the right side padding.
      • rightPad

        public static java.lang.String rightPad​(java.lang.String value,
                                                java.lang.String paddingValue,
                                                int paddingSize)
        Complete the right side of the value with n instance of the padding value.
        Parameters:
        value - Value to be completed.
        paddingValue - Padding value.
        paddingSize - Number of instance to pad the value.
        Returns:
        New string with the right side padding.
      • allIndexOf

        public static java.util.Set<java.lang.Integer> allIndexOf​(java.lang.String value,
                                                                  java.lang.String foundedValue)
        Return all the index into the string value where found the specific founded value.
        Parameters:
        value - Value to found the index.
        foundedValue - Founded value into the string.
        Returns:
        Return a list with the indexes of the places where found value.
      • allIndexOf

        public static java.util.Set<java.lang.Integer> allIndexOf​(java.lang.String value,
                                                                  java.lang.String foundedValue,
                                                                  boolean desc)
        Return all the index into the string value where found the specific founded value.
        Parameters:
        value - Value to found the index.
        foundedValue - Founded value into the string.
        desc - If this parameter is true then the first index is the smaller else if the parameter s false then the first index is bigger.
        Returns:
        Return a list with the indexes of the places where found value.
      • groupRichText

        public static java.util.List<java.lang.String> groupRichText​(java.lang.String value)
        Returns all the rich texts contained into the value and the value (in the last place) with all the replaceable places for each rich text.
        Parameters:
        value - Value to get the rich texts.
        Returns:
        List with the rich text and the original value.
      • group

        public static java.util.List<java.lang.String> group​(java.lang.String value)
        Return the list with all the groups and sub groups of the value. A group is the char sequence between the start group character '(' and the end group character ')'
        Parameters:
        value - Groupable value.
        Returns:
        List with all the groups.
      • replaceableGroup

        public static final java.util.List<java.lang.String> replaceableGroup​(java.lang.String value)
        Return a list with all groups and sub groups in ascendant order with replacement places that refer some index into the same list. e.g. "Hello (world)" - ["world", "Hello $0"]
        Parameters:
        value - String to group.
        Returns:
        List with groups.
      • getGroupIndex

        public static java.lang.String getGroupIndex​(java.lang.String value,
                                                     java.lang.String groupIndicator)
        Return the first replaceable index founded.
        Parameters:
        value - Value to analyse
        Returns:
        Replaceable index.
      • reverseRichTextGrouping

        public static java.lang.String reverseRichTextGrouping​(java.lang.String value,
                                                               java.util.List<java.lang.String> richTextGroups)
        Reverts the grouping action over the specific value.
        Parameters:
        value - Value to revert.
        richTextGroups - Group lists.
        Returns:
        Reverted value.
      • reverseGrouping

        public static java.lang.String reverseGrouping​(java.lang.String value,
                                                       java.util.List<java.lang.String> groups)
        Reverts the grouping action over the specific value.
        Parameters:
        value - Value to revert.
        groups - Group lists.
        Returns:
        Reverted value.
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes)
        Returns a hexadecimal representation of the byte array.
        Parameters:
        bytes - Byte array to represents.
        Returns:
        Hexadecimal representation.
      • hexToBytes

        public static byte[] hexToBytes​(java.lang.String hex)
        Returns a byte array based on the hexadecimal representation.
        Parameters:
        hex - Hexadecimal representation.
        Returns:
        Byte array.
      • splitByLength

        public static java.lang.String[] splitByLength​(java.lang.String value,
                                                       int length)
        This method splits the string value in n substring with the same length except for the last substring that could be smaller than the rest of the substrings.
        Parameters:
        value - String value to split.
        length - Length of the substrings.
        Returns:
        Substrings array.
      • createUUIDFromStringHash

        public static java.util.UUID createUUIDFromStringHash​(java.lang.String value)
        This method creates a uuid from a hash code of the string object.
        Parameters:
        value - String to create a uuid. The hash code of this value is used to create the least significant bits of the uuid and the most significant bits are 0.
        Returns:
        UUID instance generated using the nex constructor new UUID(0, value.hashCode());
      • createUUIDFromStringHash

        public static java.util.UUID createUUIDFromStringHash​(java.lang.String value1,
                                                              java.lang.String value2)
        This method creates a uuid from a hash code of the string object.
        Parameters:
        value1 - First string to create a uuid. The hash code of this value is used to create the most significant bits of the uuid. This value con be null.
        value2 - Second string to create a uuid. The hash code of this value is used to create the least significant bits of the uuid
        Returns:
        UUID instance generated using the nex constructor new UUID(0, value.hashCode());
      • deductInstance

        public static java.lang.Object deductInstance​(java.lang.String value)
        Returns the instance of the class that is deducted from the string value.
        Parameters:
        value - String value.
        Returns:
        Instance deducted.
      • createTaggedMessage

        public static final java.lang.String createTaggedMessage​(java.lang.String message,
                                                                 java.lang.String... tags)
        This method create a tagged message with the format $@{tag,tag,tag...}message
        Parameters:
        message - Message to tag.
        tags - Array of tags.
        Returns:
        Returns the tagged message.
      • getTagsFromMessage

        public static final java.util.Map<java.lang.String,​java.lang.String> getTagsFromMessage​(java.lang.String taggedMessage)
        This method create a hash map indexing the same message with the different tags.
        Parameters:
        taggedMessage - Tagged message.
        Returns:
        Hash map with the tags.
      • getNoMatchPlace

        public static final int getNoMatchPlace​(java.util.regex.Matcher matcher,
                                                java.lang.String value)
        Returns the place where the regex is no matching with the value.
        Parameters:
        matcher - Matcher instance.
        value - Value to found.
        Returns:
        Returns the index into the value where the regex is not matching.
      • getNearFrom

        public static final java.lang.String getNearFrom​(java.lang.String value,
                                                         int position,
                                                         int length)
        Returns a shorted string that the original centered into the position parameter and with
        Parameters:
        value -
        position -
        length -
        Returns: