Class Strings


  • public class Strings
    extends java.lang.Object
    Utilities to work with String.

    Author:
    Javier Paniza
    • Constructor Summary

      Constructors 
      Constructor Description
      Strings()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String change​(java.lang.String string, java.lang.String original, java.lang.String newString)
      Change in string original by newString.
      static java.lang.String change​(java.lang.String string, java.util.Map toChange)
      In the sent string changes the strings in toChange map for its values.
      static java.lang.String changeSeparatorsBySpaces​(java.lang.String string)
      Changes \n, \r and \t by space.
      static java.lang.String concat​(java.lang.String separator, java.lang.String... strings)
      Concatenates the list of strings using the separator.
      static java.util.Collection<java.lang.String> extractVariables​(java.lang.String text)
      Extract variables names inside ${} from a text.
      static java.util.Collection<java.lang.String> extractVariables​(java.lang.String text, java.lang.String startDelimiter, java.lang.String endDelimiter)
      Extract variables names inside the indicate separators from a text.
      static java.lang.String firstLower​(java.lang.String s)
      Returns a string like the sent one but with the first letter in lowercase.
      static java.lang.String firstToken​(java.lang.String string, java.lang.String delim)  
      static java.lang.String firstUpper​(java.lang.String s)
      Returns a string like the sent one but with the first letter in uppercase.
      static java.lang.String fix​(java.lang.String string, int length, Align align)
      Fix the length of the string filling with spaces if needed.
      static java.lang.String fix​(java.lang.String string, int length, Align align, char fillCharacter)
      Fix the length of the string filling with the specified character if needed.
      static boolean isModelName​(java.lang.String string)
      Determines if the string is a valid model name.
      static boolean isNumeric​(java.lang.CharSequence string)
      The space, comma, dot, + and - are considered as numeric.
      static java.lang.String javaIdentifierToNaturalLabel​(java.lang.String name)
      Convert a string with a Java identifier in label natural for a human.
      static java.lang.String lastToken​(java.lang.String string)  
      static java.lang.String lastToken​(java.lang.String string, java.lang.String delim)  
      static java.lang.String multiline​(java.lang.String... strings)
      Returns a String multiline platform independent.
      static java.lang.String naturalLabelToIdentifier​(java.lang.String naturalLabel)
      Convert a string with a label natural for a human into a identifier valid to use as URL, file name, internal id, etc.
      static java.lang.String noFirstToken​(java.lang.String string, java.lang.String delim)
      All string but without first token.
      static java.lang.String noFirstTokenWithoutFirstDelim​(java.lang.String string, java.lang.String delim)
      All string but without first token.
      static java.lang.String noLastToken​(java.lang.String string)
      All string but without last token.
      static java.lang.String noLastToken​(java.lang.String string, java.lang.String delim)
      All string but without last token.
      static java.lang.String noLastTokenWithoutLastDelim​(java.lang.String string, java.lang.String delim)
      All string but without last token.
      static java.lang.String removeAccents​(java.lang.String value)
      Change from a vowel with an accent, to vowel with no accent If you send "Camión" it returns "Camion"
      static java.lang.String removeBlanks​(java.lang.String string)
      Returns the argument string without blanks (\n, \r, \t, \f) or whitespace.
      static java.lang.Object removeXSS​(java.lang.Object notSafeValue)  
      static java.lang.String removeXSS​(java.lang.String notSafeValue)
      A key function of any application filtering process will be the removal of possible dangerous special characters.
      static java.lang.String repeat​(int count, java.lang.String string)
      Creates a string from repeating another string.
      static java.lang.String spaces​(int count)
      Creates a string with the specified blank spaces.
      static java.lang.String[] toArray​(java.lang.String list)
      Converts a list of comma separated elements in a string array.
      static java.lang.String[] toArray​(java.lang.String list, java.lang.String separator)
      Converts a list of elements separated by a arbitrary character in a string array.
      static java.lang.String toCharSet​(java.lang.String original, java.lang.String charSet)
      Translate to the charset specified.
      static java.util.Collection<java.lang.String> toCollection​(java.lang.String list)
      Converts a list of comma separated elements in a string collection.
      static java.util.Collection<java.lang.String> toCollection​(java.lang.String list, java.lang.String separator)
      Converts a list of elements separated by a arbitrary character in a string collection.
      static java.util.List<java.lang.String> toList​(java.lang.String list)
      Converts a list of comma separated elements in a string List.
      static java.util.List<java.lang.String> toList​(java.lang.String list, java.lang.String separator)
      Converts a list of elements separated by a arbitrary character in a string List.
      static java.lang.Object toObject​(java.lang.Class type, java.lang.String string)
      Converts a string a object of the specified type.
      static java.util.Set<java.lang.String> toSet​(java.lang.String list)
      Converts a list of comma separated elements in a string set.
      static java.util.Set<java.lang.String> toSet​(java.lang.String list, java.lang.String separator)
      Converts a list of elements separated by a arbitrary character in a string set.
      static java.util.Set<java.lang.String> toSetNullByPass​(java.lang.String list)
      Converts a list of comma separated elements in a string set.
      static java.lang.String toString​(java.lang.Object object)
      Try to do a decent toString from a regular object.
      static java.lang.String toString​(java.lang.Object[] array)
      Converts an array of objects in a string of comma separated elements.
      static java.lang.String toString​(java.lang.Object[] array, java.lang.String separator)
      Converts an array of objects in string of elements separated by a arbitrary character .
      static java.lang.String toString​(java.util.Collection collection)
      Converts a collection of objects in a string of comma separated elements.
      static java.lang.String toString​(java.util.Collection collection, java.lang.String separator)
      Converts a collection of objects in string of elements separated by a arbitrary character .
      static java.lang.String unquote​(java.lang.String sentence)
      Remove the quotes from a sentence between quotes.
      • 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

      • concat

        public static java.lang.String concat​(java.lang.String separator,
                                              java.lang.String... strings)
        Concatenates the list of strings using the separator.

        For example, concat(" - ", "Juan", "Perico", "Andrés") returns "Juan - Perico - Andrés".

        Parameters:
        separator - The character used as separator.
        strings - Strings to be concatenated. Can be null.
        Returns:
        Not null, including the case strings == null.
        Since:
        6.0
      • isNumeric

        public static boolean isNumeric​(java.lang.CharSequence string)
        The space, comma, dot, + and - are considered as numeric.
        Since:
        5.1.1
      • changeSeparatorsBySpaces

        public static java.lang.String changeSeparatorsBySpaces​(java.lang.String string)
        Changes \n, \r and \t by space.

        Parameters:
        string - Not null
      • toCharSet

        public static java.lang.String toCharSet​(java.lang.String original,
                                                 java.lang.String charSet)
                                          throws java.io.UnsupportedEncodingException
        Translate to the charset specified.

        Parameters:
        original - Original string
        charSet - Charset to traslate to, for example, UTF-8, or ISO-8859-1
        Returns:
        The string translated
        Throws:
        java.io.UnsupportedEncodingException - If charset is not supported
      • spaces

        public static java.lang.String spaces​(int count)
        Creates a string with the specified blank spaces.

        Parameters:
        count - Quantity of spaces
        Returns:
        Not null.
      • fix

        public static java.lang.String fix​(java.lang.String string,
                                           int length,
                                           Align align)
        Fix the length of the string filling with spaces if needed.

        Returns the sent string but with the specified length.
        It fills or cuts as it needs.

        Parameters:
        string - Can be null, in which case empty string is assumed
        length - Character count of result string
        align - Not null
        Returns:
        Not null
      • fix

        public static java.lang.String fix​(java.lang.String string,
                                           int length,
                                           Align align,
                                           char fillCharacter)
        Fix the length of the string filling with the specified character if needed.

        Returns the sent string but with the specified length.
        It fills or cuts as it needs.

        Parameters:
        string - Can be null, in which case empty string is assumed
        length - Character count of result string
        align - Not null
        fillCharacter - Character used to fill
        Returns:
        Not null
      • repeat

        public static java.lang.String repeat​(int count,
                                              java.lang.String string)
        Creates a string from repeating another string.

        Parameters:
        count - Times to repeat
        string - String to repeat
        Returns:
        Not null
      • toArray

        public static final java.lang.String[] toArray​(java.lang.String list)
        Converts a list of comma separated elements in a string array.

        For example, the list Angel, Manolo, Antonia is converted to a array of 3 elements with this 3 names without comman nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        Returns:
        Not null, including the case list == null.
      • toArray

        public static final java.lang.String[] toArray​(java.lang.String list,
                                                       java.lang.String separator)
        Converts a list of elements separated by a arbitrary character in a string array.

        For example, the list Angel : Manolo : Antonia is converted to a array of 3 elements with this 3 names without colon (for example) nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        separator - The character used as separator.
        Returns:
        Not null, including the case list == null.
      • toCollection

        public static final java.util.Collection<java.lang.String> toCollection​(java.lang.String list)
        Converts a list of comma separated elements in a string collection.

        For example, the list Angel, Manolo, Antonia is converted to a collection of 3 elements with this 3 names without comman nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        Returns:
        Not null, including the case list == null.
      • toCollection

        public static final java.util.Collection<java.lang.String> toCollection​(java.lang.String list,
                                                                                java.lang.String separator)
        Converts a list of elements separated by a arbitrary character in a string collection.

        For example, the list Angel : Manolo : Antonia is converted to a collection of 3 elements with this 3 names without colon (for example) nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        separator - The character used as separator.
        Returns:
        Not null, including the case list == null.
      • toList

        public static final java.util.List<java.lang.String> toList​(java.lang.String list)
        Converts a list of comma separated elements in a string List.

        For example, the list Angel, Manolo, Antonia is converted to a List of 3 elements with this 3 names without comma nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        Returns:
        Not null, including the case list == null.
        Since:
        4.3
      • toList

        public static final java.util.List<java.lang.String> toList​(java.lang.String list,
                                                                    java.lang.String separator)
        Converts a list of elements separated by a arbitrary character in a string List.

        For example, the list Angel : Manolo : Antonia is converted to a List of 3 elements with this 3 names without colon (for example) nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        separator - The character used as separator.
        Returns:
        Not null, including the case list == null.
        Since:
        4.3
      • toSet

        public static final java.util.Set<java.lang.String> toSet​(java.lang.String list)
        Converts a list of comma separated elements in a string set.

        For example, the list Angel, Manolo, Antonia is converted to a set of 3 elements with this 3 names without comman nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        Returns:
        Not null, including the case list == null.
        Since:
        4.1
      • toSetNullByPass

        public static final java.util.Set<java.lang.String> toSetNullByPass​(java.lang.String list)
        Converts a list of comma separated elements in a string set.

        For example, the list Angel, Manolo, Antonia is converted to a set of 3 elements with this 3 names without comman nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        Returns:
        Null in the case list == null.
        Since:
        4.1
      • toSet

        public static final java.util.Set<java.lang.String> toSet​(java.lang.String list,
                                                                  java.lang.String separator)
        Converts a list of elements separated by a arbitrary character in a string set.

        For example, the list Angel : Manolo : Antonia is converted to a set of 3 elements with this 3 names without colon (for example) nor spaces.

        Parameters:
        list - String with the list. If null return a empty string
        separator - The character used as separator.
        Returns:
        Not null, including the case list == null.
        Since:
        4.1
      • toString

        public static final java.lang.String toString​(java.util.Collection collection)
        Converts a collection of objects in a string of comma separated elements.

        For example, a collection of 3 elements with 3 names is converted to the string Angel, Manolo, Antonia

        Parameters:
        collection - Collection with the elements. If null return an empty string
        Returns:
        Not null, including the case collection == null.
      • toString

        public static final java.lang.String toString​(java.util.Collection collection,
                                                      java.lang.String separator)
        Converts a collection of objects in string of elements separated by a arbitrary character .

        For example, a collection of 3 elements with this 3 names is converted to a string of 3 elements with this 3 names and colon (for example).

        Parameters:
        collection - A collection of objects. If null return an empty string
        separator - The character used as separator.
        Returns:
        Not null, including the case collection == null.
      • toString

        public static final java.lang.String toString​(java.lang.Object[] array)
        Converts an array of objects in a string of comma separated elements.

        For example, an array of 3 elements with 3 names is converted to the string Angel, Manolo, Antonia

        Parameters:
        array - Array with the elements. If null return a empty string
        Returns:
        Not null, including the case array == null.
        Since:
        5.6
      • toString

        public static final java.lang.String toString​(java.lang.Object[] array,
                                                      java.lang.String separator)
        Converts an array of objects in string of elements separated by a arbitrary character .

        For example, an array of 3 elements with this 3 names is converted to a string of 3 elements with this 3 names and colon (for example).

        Parameters:
        array - An array of objects. If null return an empty string
        separator - The character used as separator.
        Returns:
        Not null, including the case array == null.
        Since:
        5.6
      • toString

        public static final java.lang.String toString​(java.lang.Object object)
        Try to do a decent toString from a regular object.

        Good format for arrays, dates, numbers, etc. taking in account the current locales (from Locales.getCurrent())

        Since:
        5.9
      • toObject

        public static final java.lang.Object toObject​(java.lang.Class type,
                                                      java.lang.String string)
        Converts a string a object of the specified type.

        Supports all primitive type plus its wrappers except char and void.
        Support String and BigDecimal too.

        If there is conversion error or is a type not supporte return null or the default value for the type (zero for numeric).

        Parameters:
        type - The type of returned object (can be a primitive type in this case return its wrapper). Not null
        string - String with data to convert. Can be null, in this case return a default value.
      • firstUpper

        public static java.lang.String firstUpper​(java.lang.String s)
        Returns a string like the sent one but with the first letter in uppercase.

        If null is sent null is returned.

      • firstLower

        public static java.lang.String firstLower​(java.lang.String s)
        Returns a string like the sent one but with the first letter in lowercase.

        If null is sent null is returned.

      • change

        public static java.lang.String change​(java.lang.String string,
                                              java.util.Map toChange)
        In the sent string changes the strings in toChange map for its values.

        Parameters:
        string - Not null
        toChagne - Not null
      • change

        public static java.lang.String change​(java.lang.String string,
                                              java.lang.String original,
                                              java.lang.String newString)
        Change in string original by newString.

        Parameters:
        string - String in which we make the changes. Can be null
        original - String to search. Not null
        newString - New value to put in place of original. Not null
        Returns:
        The changed string, if the sent string is null a null is returned
      • lastToken

        public static java.lang.String lastToken​(java.lang.String string)
        Returns:
        If string if null or have no tokens returns empty string.
      • lastToken

        public static java.lang.String lastToken​(java.lang.String string,
                                                 java.lang.String delim)
        Returns:
        If string if null or have no tokens returns empty string.
      • noLastToken

        public static java.lang.String noLastToken​(java.lang.String string)
        All string but without last token.

        A trim is applied to the result.

        Returns:
        If string if null or have no tokens returns empty string.
      • noLastToken

        public static java.lang.String noLastToken​(java.lang.String string,
                                                   java.lang.String delim)
        All string but without last token.

        Includes the last delim.

        Returns:
        If string if null or have no tokens returns empty string.
      • noFirstToken

        public static java.lang.String noFirstToken​(java.lang.String string,
                                                    java.lang.String delim)
        All string but without first token.

        Includes the delim.

        Returns:
        If string if null or have no tokens returns empty string.
      • noFirstTokenWithoutFirstDelim

        public static java.lang.String noFirstTokenWithoutFirstDelim​(java.lang.String string,
                                                                     java.lang.String delim)
        All string but without first token.

        Does not include the delim.

        Returns:
        If string if null or have no tokens returns empty string.
      • noLastTokenWithoutLastDelim

        public static java.lang.String noLastTokenWithoutLastDelim​(java.lang.String string,
                                                                   java.lang.String delim)
        All string but without last token.

        It does not include the last delim.

        Returns:
        If string if null or have no tokens returns empty string.
      • firstToken

        public static java.lang.String firstToken​(java.lang.String string,
                                                  java.lang.String delim)
        Returns:
        If string if null or have no tokens returns empty string.
      • removeXSS

        public static java.lang.String removeXSS​(java.lang.String notSafeValue)
        A key function of any application filtering process will be the removal of possible dangerous special characters.
        Returns:
        new safe string
      • removeXSS

        public static java.lang.Object removeXSS​(java.lang.Object notSafeValue)
        Parameters:
        notSafeValue -
        Returns:
        Safe Object
      • javaIdentifierToNaturalLabel

        public static java.lang.String javaIdentifierToNaturalLabel​(java.lang.String name)
        Convert a string with a Java identifier in label natural for a human.

        If you send "firstName" it returns "First name".
        If you send "CustomerOrder" it returns "Customer order".

      • naturalLabelToIdentifier

        public static java.lang.String naturalLabelToIdentifier​(java.lang.String naturalLabel)
        Convert a string with a label natural for a human into a identifier valid to use as URL, file name, internal id, etc.

        If you send "León, España" it returns "LeonEspana".
        The % is changed by the "percent" string.

        Since:
        5.6
      • removeAccents

        public static java.lang.String removeAccents​(java.lang.String value)
        Change from a vowel with an accent, to vowel with no accent If you send "Camión" it returns "Camion"
        Since:
        v4m6
      • isModelName

        public static boolean isModelName​(java.lang.String string)
        Determines if the string is a valid model name.
        Since:
        4.5
      • removeBlanks

        public static java.lang.String removeBlanks​(java.lang.String string)
        Returns the argument string without blanks (\n, \r, \t, \f) or whitespace.

        If you send "my group" it returns "myGroup".
        If you send "My Section" it returns "MySection".

        Parameters:
        string - String not null
        Since:
        5.2.1
      • multiline

        public static java.lang.String multiline​(java.lang.String... strings)
        Returns a String multiline platform independent.

        For example,

         Strings.multiline("OpenXava", "AJAX Java Framework Web", "You only have to write the domain classes") ->
            "OpenXava
             AJAX Java Framework Web
             You only have to write the domain classes"
        Parameters:
        strings - The array of String objects, entries not may be null
        Since:
        5.7
      • unquote

        public static java.lang.String unquote​(java.lang.String sentence)
        Remove the quotes from a sentence between quotes.

        That is:

         "Hi, I'm Peter" --> Hi, I'm Peter
         
        Parameters:
        sentence - The original sentence, with or without surrounding quotes.
        Returns:
        The sentence without quotes, if the sentence has no quotes returns the original string.
        Since:
        5.8
      • extractVariables

        public static java.util.Collection<java.lang.String> extractVariables​(java.lang.String text)
        Extract variables names inside ${} from a text.

        So, if you have "select ${number}, ${name} from ${customer} you get a collection with the strings "number", "name" and "customer".

        Parameters:
        text - Text to examine, can be null.
        Returns:
        The collection with the variable names, it never will be null.
        Since:
        6.5.1
      • extractVariables

        public static java.util.Collection<java.lang.String> extractVariables​(java.lang.String text,
                                                                              java.lang.String startDelimiter,
                                                                              java.lang.String endDelimiter)
        Extract variables names inside the indicate separators from a text.

        So, if you have "select [number], [name] from [customer] and call sending "[" and "]" as delimiter, you get a collection with the strings "number", "name" and "customer".

        Parameters:
        text - Text to examine, can be null.
        endDelimiter - The delimiter that finished a variable, like } or ].
        Returns:
        The collection with the variable names, it never will be null.
        Since:
        6.5.1