Class Strings


  • public final class Strings
    extends java.lang.Object
    A collection of String utilities.
    Version:
    $Revision$
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Strings.Range
      Represents a range between two integers.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String[] ejbQlIdentifiers
      List of EJB-QL Identifiers as defined in the EJB 2.0 Specification Section 11.2.6.1
      static java.lang.String EMPTY
      An empty string constant
      private static long HOUR  
      private static java.lang.String[] keywords
      List of valid Java keywords, see The Java Language Specification Second Edition Section 3.9, 3.10.3 and 3.10.7
      private static long MINS  
      private static long MSEC
      Millisecond conversion constants
      private static long SECS  
    • Constructor Summary

      Constructors 
      Constructor Description
      Strings()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String capitalize​(java.lang.String string)
      Capitalize the first character of the given string.
      static boolean compare​(java.lang.String me, java.lang.String you)
      Compare two strings.
      static int count​(java.lang.String string, char c)
      Count the number of instances of character within a string.
      static int count​(java.lang.String string, java.lang.String substring)
      Count the number of instances of substring within a string.
      private static java.lang.String decode​(java.lang.String filespec)  
      static java.lang.String defaultToString​(java.lang.Object object)
      The default toString implementation of an object
      static void defaultToString​(java.lang.StringBuffer buffer, java.lang.Object object)
      The default toString implementation of an object
      static void defaultToString​(JBossStringBuilder buffer, java.lang.Object object)
      The default toString implementation of an object
      static boolean hasLength​(java.lang.String string)
      Check that the given string param is neither null nor of length 0.
      static boolean isEjbQlIdentifier​(java.lang.String s)
      Check whether the given String is an identifier according to the EJB-QL definition.
      static boolean isEmpty​(java.lang.String string)
      Check if the given string is empty.
      static boolean isJavaKeyword​(java.lang.String s)
      Check whether the given String is a reserved Java Keyword according to the Java Language Specifications.
      static boolean isValidJavaIdentifier​(java.lang.String s)
      Check whether the given String is a valid identifier according to the Java Language specifications.
      static java.lang.String join​(byte[] array)
      Convert and join an array of bytes into one string.
      static java.lang.String join​(java.lang.Object[] array)
      Convert and join an array of objects into one string.
      static java.lang.String join​(java.lang.Object[] array, java.lang.String delim)
      Join an array of strings into one delimited string.
      static java.lang.String join​(java.lang.Object[] array, java.lang.String prefix, java.lang.String separator, java.lang.String suffix)
      Return a string composed of the given array.
      static java.lang.String join​(java.lang.StringBuffer buff, java.lang.Object[] array, java.lang.String delim)
      Join an array of strings into one delimited string.
      static java.lang.String join​(java.lang.StringBuffer buff, java.lang.Object[] array, java.lang.String prefix, java.lang.String separator, java.lang.String suffix)
      Return a string composed of the given array.
      private static java.net.URI makeURIFromFilespec​(java.lang.String filespec, java.lang.String relativePrefix)  
      private static java.net.URL makeURLFromFilespec​(java.lang.String filespec, java.lang.String relativePrefix)
      A helper to make a URL from a filespec.
      static int nthIndexOf​(java.lang.String string, java.lang.String token, int index)
      Return the nth index of the given token occurring in the given string.
      static java.lang.String pad​(java.lang.Object obj, int count)
      Return a string padded with the given string value of an object for the given count.
      static java.lang.String pad​(java.lang.StringBuffer buff, java.lang.String string, int count)
      Return a string padded with the given string for the given count.
      static java.lang.String pad​(java.lang.String string, int count)
      Return a string padded with the given string for the given count.
      static java.util.Locale parseLocaleString​(java.lang.String localeString)
      Parse the given localeString into a Locale.
      static long parsePositiveTimePeriod​(java.lang.String period)
      Same like parseTimePeriod(), but guards for negative entries.
      static long parseTimePeriod​(java.lang.String period)
      Parses a time period into a long.
      static Strings.Range rangeOf​(java.lang.String beginToken, java.lang.String endToken, java.lang.String string)
      Return the range from a begining token to an ending token.
      static Strings.Range rangeOf​(java.lang.String beginToken, java.lang.String endToken, java.lang.String string, int fromIndex)
      Return the range from a begining token to an ending token.
      static java.lang.String removeWhiteSpace​(java.lang.String s)
      Returns a new string with all the whitespace removed
      static java.lang.String[] split​(java.lang.String string, java.lang.String delim)
      Split up a string into multiple strings based on a delimiter.
      static java.lang.String[] split​(java.lang.String string, java.lang.String delim, int limit)
      Split up a string into multiple strings based on a delimiter.
      static java.lang.String subst​(java.lang.StringBuffer buff, java.lang.String string, java.lang.String[] replace, char token)
      Substitute index identifiers with the replacement value from the given array for the corresponding index.
      static java.lang.String subst​(java.lang.StringBuffer buff, java.lang.String from, java.lang.String to, java.lang.String string)
      Substitute sub-strings in side of a string.
      static java.lang.String subst​(java.lang.StringBuffer buff, java.lang.String string, java.util.Map map, java.lang.String beginToken, java.lang.String endToken)
      Substitute sub-strings in side of a string.
      static java.lang.String subst​(java.lang.String string, java.lang.String[] replace)
      Substitute index identifiers (with % for the index token) with the replacement value from the given array for the corresponding index.
      static java.lang.String subst​(java.lang.String string, java.lang.String[] replace, char token)
      Substitute index identifiers with the replacement value from the given array for the corresponding index.
      static java.lang.String subst​(java.lang.String from, java.lang.String to, java.lang.String string)
      Substitute sub-strings in side of a string.
      static java.lang.String subst​(java.lang.String string, java.util.Map map, java.lang.String beginToken, java.lang.String endToken)
      Substitute sub-strings in side of a string.
      static java.lang.String[] tokenizeToStringArray​(java.lang.String str, java.lang.String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
      Tokenize the given String into a String array via a StringTokenizer.
      static java.net.URI toURI​(java.lang.String urispec)  
      static java.net.URI toURI​(java.lang.String urispec, java.lang.String relativePrefix)  
      static java.net.URL toURL​(java.lang.String urlspec)
      Make a URL from the given string.
      static java.net.URL toURL​(java.lang.String urlspec, java.lang.String relativePrefix)
      Make a URL from the given string.
      static java.lang.String[] trim​(java.lang.String[] strings)
      Trim each string in the given string array.
      static java.lang.String trimLeadingCharacter​(java.lang.String str, char leadingCharacter)
      Trim all occurences of the supplied leading character from the given String.
      static java.lang.String trimLeadingCharacter​(java.lang.String str, CharacterChecker checker)
      Trim all occurences of the supplied leading character from the given String.
      static java.lang.String trimLeadingWhitespace​(java.lang.String str)
      Trim leading whitespace from the given String.
      • Methods inherited from class java.lang.Object

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

      • EMPTY

        public static final java.lang.String EMPTY
        An empty string constant
        See Also:
        Constant Field Values
      • keywords

        private static final java.lang.String[] keywords
        List of valid Java keywords, see The Java Language Specification Second Edition Section 3.9, 3.10.3 and 3.10.7
      • ejbQlIdentifiers

        private static final java.lang.String[] ejbQlIdentifiers
        List of EJB-QL Identifiers as defined in the EJB 2.0 Specification Section 11.2.6.1
    • Constructor Detail

      • Strings

        public Strings()
    • Method Detail

      • subst

        public static java.lang.String subst​(java.lang.StringBuffer buff,
                                             java.lang.String from,
                                             java.lang.String to,
                                             java.lang.String string)
        Substitute sub-strings in side of a string.
        Parameters:
        buff - Stirng buffer to use for substitution (buffer is not reset)
        from - String to substitute from
        to - String to substitute to
        string - String to look for from in
        Returns:
        Substituted string
      • subst

        public static java.lang.String subst​(java.lang.String from,
                                             java.lang.String to,
                                             java.lang.String string)
        Substitute sub-strings in side of a string.
        Parameters:
        from - String to substitute from
        to - String to substitute to
        string - String to look for from in
        Returns:
        Substituted string
      • subst

        public static java.lang.String subst​(java.lang.StringBuffer buff,
                                             java.lang.String string,
                                             java.util.Map map,
                                             java.lang.String beginToken,
                                             java.lang.String endToken)
        Substitute sub-strings in side of a string.
        Parameters:
        buff - String buffer to use for substitution (buffer is not reset)
        string - String to subst mappings in
        map - Map of from->to strings
        beginToken - Beginning token
        endToken - Ending token
        Returns:
        Substituted string
      • subst

        public static java.lang.String subst​(java.lang.String string,
                                             java.util.Map map,
                                             java.lang.String beginToken,
                                             java.lang.String endToken)
        Substitute sub-strings in side of a string.
        Parameters:
        string - String to subst mappings in
        map - Map of from->to strings
        beginToken - Beginning token
        endToken - Ending token
        Returns:
        Substituted string
      • subst

        public static java.lang.String subst​(java.lang.StringBuffer buff,
                                             java.lang.String string,
                                             java.lang.String[] replace,
                                             char token)
        Substitute index identifiers with the replacement value from the given array for the corresponding index.
        Parameters:
        buff - The string buffer used for the substitution (buffer is not reset).
        string - String substitution format.
        replace - Array of strings whose values will be used as replacements in the given string when a token with their index is found.
        token - The character token to specify the start of an index reference.
        Returns:
        Substituted string.
      • subst

        public static java.lang.String subst​(java.lang.String string,
                                             java.lang.String[] replace,
                                             char token)
        Substitute index identifiers with the replacement value from the given array for the corresponding index.
        Parameters:
        string - String substitution format.
        replace - Array of strings whose values will be used as replacements in the given string when a token with their index is found.
        token - The character token to specify the start of an index reference.
        Returns:
        Substituted string.
      • subst

        public static java.lang.String subst​(java.lang.String string,
                                             java.lang.String[] replace)
        Substitute index identifiers (with % for the index token) with the replacement value from the given array for the corresponding index.
        Parameters:
        string - String substitution format.
        replace - Array of strings whose values will be used as replacements in the given string when a token with their index is found.
        Returns:
        Substituted string.
      • rangeOf

        public static Strings.Range rangeOf​(java.lang.String beginToken,
                                            java.lang.String endToken,
                                            java.lang.String string,
                                            int fromIndex)
        Return the range from a begining token to an ending token.
        Parameters:
        beginToken - String to indicate begining of range.
        endToken - String to indicate ending of range.
        string - String to look for range in.
        fromIndex - Beginning index.
        Returns:
        (begin index, end index) or null.
      • rangeOf

        public static Strings.Range rangeOf​(java.lang.String beginToken,
                                            java.lang.String endToken,
                                            java.lang.String string)
        Return the range from a begining token to an ending token.
        Parameters:
        beginToken - String to indicate begining of range.
        endToken - String to indicate ending of range.
        string - String to look for range in.
        Returns:
        (begin index, end index) or null.
      • split

        public static java.lang.String[] split​(java.lang.String string,
                                               java.lang.String delim,
                                               int limit)
        Split up a string into multiple strings based on a delimiter.
        Parameters:
        string - String to split up.
        delim - Delimiter.
        limit - Limit the number of strings to split into (-1 for no limit).
        Returns:
        Array of strings.
      • split

        public static java.lang.String[] split​(java.lang.String string,
                                               java.lang.String delim)
        Split up a string into multiple strings based on a delimiter.
        Parameters:
        string - String to split up.
        delim - Delimiter.
        Returns:
        Array of strings.
      • join

        public static java.lang.String join​(java.lang.StringBuffer buff,
                                            java.lang.Object[] array,
                                            java.lang.String delim)
        Join an array of strings into one delimited string.
        Parameters:
        buff - String buffered used for join (buffer is not reset).
        array - Array of objects to join as strings.
        delim - Delimiter to join strings with or null.
        Returns:
        Joined string.
      • join

        public static java.lang.String join​(java.lang.Object[] array,
                                            java.lang.String delim)
        Join an array of strings into one delimited string.
        Parameters:
        array - Array of objects to join as strings.
        delim - Delimiter to join strings with or null.
        Returns:
        Joined string.
      • join

        public static java.lang.String join​(java.lang.Object[] array)
        Convert and join an array of objects into one string.
        Parameters:
        array - Array of objects to join as strings.
        Returns:
        Converted and joined objects.
      • join

        public static java.lang.String join​(byte[] array)
        Convert and join an array of bytes into one string.
        Parameters:
        array - Array of objects to join as strings.
        Returns:
        Converted and joined objects.
      • join

        public static java.lang.String join​(java.lang.StringBuffer buff,
                                            java.lang.Object[] array,
                                            java.lang.String prefix,
                                            java.lang.String separator,
                                            java.lang.String suffix)
        Return a string composed of the given array.
        Parameters:
        buff - Buffer used to construct string value (not reset).
        array - Array of objects.
        prefix - String prefix.
        separator - Element sepearator.
        suffix - String suffix.
        Returns:
        String in the format of: prefix + n ( + separator + n+i)* + suffix.
      • join

        public static java.lang.String join​(java.lang.Object[] array,
                                            java.lang.String prefix,
                                            java.lang.String separator,
                                            java.lang.String suffix)
        Return a string composed of the given array.
        Parameters:
        array - Array of objects.
        prefix - String prefix.
        separator - Element sepearator.
        suffix - String suffix.
        Returns:
        String in the format of: prefix + n ( + separator + n+i)* + suffix.
      • count

        public static int count​(java.lang.String string,
                                java.lang.String substring)
        Count the number of instances of substring within a string.
        Parameters:
        string - String to look for substring in.
        substring - Sub-string to look for.
        Returns:
        Count of substrings in string.
      • count

        public static int count​(java.lang.String string,
                                char c)
        Count the number of instances of character within a string.
        Parameters:
        string - String to look for substring in.
        c - Character to look for.
        Returns:
        Count of substrings in string.
      • pad

        public static java.lang.String pad​(java.lang.StringBuffer buff,
                                           java.lang.String string,
                                           int count)
        Return a string padded with the given string for the given count.
        Parameters:
        buff - String buffer used for padding (buffer is not reset).
        string - Pad element.
        count - Pad count.
        Returns:
        Padded string.
      • pad

        public static java.lang.String pad​(java.lang.String string,
                                           int count)
        Return a string padded with the given string for the given count.
        Parameters:
        string - Pad element.
        count - Pad count.
        Returns:
        Padded string.
      • pad

        public static java.lang.String pad​(java.lang.Object obj,
                                           int count)
        Return a string padded with the given string value of an object for the given count.
        Parameters:
        obj - Object to convert to a string.
        count - Pad count.
        Returns:
        Padded string.
      • compare

        public static boolean compare​(java.lang.String me,
                                      java.lang.String you)

        Compare two strings.

        Both or one of them may be null.

        Parameters:
        me -
        you -
        Returns:
        true if object equals or intern ==, else false.
      • isEmpty

        public static boolean isEmpty​(java.lang.String string)
        Check if the given string is empty.
        Parameters:
        string - String to check
        Returns:
        True if string is empty
      • nthIndexOf

        public static int nthIndexOf​(java.lang.String string,
                                     java.lang.String token,
                                     int index)
        Return the nth index of the given token occurring in the given string.
        Parameters:
        string - String to search.
        token - Token to match.
        index - Nth index.
        Returns:
        Index of nth item or -1.
      • capitalize

        public static java.lang.String capitalize​(java.lang.String string)
        Capitalize the first character of the given string.
        Parameters:
        string - String to capitalize.
        Returns:
        Capitalized string.
        Throws:
        java.lang.IllegalArgumentException - String is null or empty.
      • trim

        public static java.lang.String[] trim​(java.lang.String[] strings)
        Trim each string in the given string array.

        This modifies the string array.

        Parameters:
        strings - String array to trim.
        Returns:
        String array with each element trimmed.
      • toURL

        public static java.net.URL toURL​(java.lang.String urlspec,
                                         java.lang.String relativePrefix)
                                  throws java.net.MalformedURLException
        Make a URL from the given string.

        If the string is a properly formatted file URL, then the file portion will be made canonical.

        If the string is an invalid URL then it will be converted into a file URL.

        Parameters:
        urlspec - The string to construct a URL for.
        relativePrefix - The string to prepend to relative file paths, or null to disable prepending.
        Returns:
        A URL for the given string.
        Throws:
        java.net.MalformedURLException - Could not make a URL for the given string.
      • toURI

        public static java.net.URI toURI​(java.lang.String urispec,
                                         java.lang.String relativePrefix)
                                  throws java.net.URISyntaxException
        Throws:
        java.net.URISyntaxException
      • makeURLFromFilespec

        private static java.net.URL makeURLFromFilespec​(java.lang.String filespec,
                                                        java.lang.String relativePrefix)
                                                 throws java.io.IOException
        A helper to make a URL from a filespec.
        Throws:
        java.io.IOException
      • decode

        private static java.lang.String decode​(java.lang.String filespec)
      • makeURIFromFilespec

        private static java.net.URI makeURIFromFilespec​(java.lang.String filespec,
                                                        java.lang.String relativePrefix)
      • toURL

        public static java.net.URL toURL​(java.lang.String urlspec)
                                  throws java.net.MalformedURLException
        Make a URL from the given string.
        Parameters:
        urlspec - The string to construct a URL for.
        Returns:
        A URL for the given string.
        Throws:
        java.net.MalformedURLException - Could not make a URL for the given string.
        See Also:
        toURL(String,String)
      • toURI

        public static java.net.URI toURI​(java.lang.String urispec)
                                  throws java.net.URISyntaxException
        Parameters:
        urispec -
        Returns:
        the uri
        Throws:
        java.net.URISyntaxException - for any error
      • isJavaKeyword

        public static final boolean isJavaKeyword​(java.lang.String s)
        Check whether the given String is a reserved Java Keyword according to the Java Language Specifications.
        Parameters:
        s - String to check
        Returns:
        true if the given String is a reserved Java keyword, false otherwise.
      • isEjbQlIdentifier

        public static final boolean isEjbQlIdentifier​(java.lang.String s)
        Check whether the given String is an identifier according to the EJB-QL definition. See The EJB 2.0 Documentation Section 11.2.6.1.
        Parameters:
        s - String to check
        Returns:
        true if the given String is a reserved identifier in EJB-QL, false otherwise.
      • isValidJavaIdentifier

        public static final boolean isValidJavaIdentifier​(java.lang.String s)
        Check whether the given String is a valid identifier according to the Java Language specifications. See The Java Language Specification Second Edition, Section 3.8 for the definition of what is a valid identifier.
        Parameters:
        s - String to check
        Returns:
        true if the given String is a valid Java identifier, false otherwise.
      • removeWhiteSpace

        public static java.lang.String removeWhiteSpace​(java.lang.String s)
        Returns a new string with all the whitespace removed
        Parameters:
        s - the source string
        Returns:
        the string without whitespace or null
      • defaultToString

        public static final java.lang.String defaultToString​(java.lang.Object object)
        The default toString implementation of an object
        Parameters:
        object - the object
        Returns:
        a string in the form className@hexHashCode
      • defaultToString

        public static final void defaultToString​(JBossStringBuilder buffer,
                                                 java.lang.Object object)
        The default toString implementation of an object
        Parameters:
        object - the object
        buffer - the string builder
      • defaultToString

        public static final void defaultToString​(java.lang.StringBuffer buffer,
                                                 java.lang.Object object)
        The default toString implementation of an object
        Parameters:
        object - the object
        buffer - the string buffer
      • parseTimePeriod

        public static long parseTimePeriod​(java.lang.String period)
        Parses a time period into a long. Translates possible [msec|sec|min|h] suffixes For example: "1" -> 1 (msec) "1msec -> 1 (msec) "1sec" -> 1000 (msecs) "1min" -> 60000 (msecs) "1h" -> 3600000 (msecs) Accepts negative periods, e.g. "-1"
        Parameters:
        period - the stringfied time period
        Returns:
        the parsed time period as long
        Throws:
        java.lang.NumberFormatException
      • parsePositiveTimePeriod

        public static long parsePositiveTimePeriod​(java.lang.String period)
        Same like parseTimePeriod(), but guards for negative entries.
        Parameters:
        period - the stringfied time period
        Returns:
        the parsed time period as long
        Throws:
        java.lang.NumberFormatException
      • tokenizeToStringArray

        public static java.lang.String[] tokenizeToStringArray​(java.lang.String str,
                                                               java.lang.String delimiters,
                                                               boolean trimTokens,
                                                               boolean ignoreEmptyTokens)
        Tokenize the given String into a String array via a StringTokenizer. The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray
        Parameters:
        str - the String to tokenize
        delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)
        trimTokens - trim the tokens via String's trim
        ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).
        Returns:
        an array of the tokens (null if the input String was null)
      • trimLeadingWhitespace

        public static java.lang.String trimLeadingWhitespace​(java.lang.String str)
        Trim leading whitespace from the given String.
        Parameters:
        str - the string to check
        Returns:
        the trimmed String
        See Also:
        Character.isWhitespace(char)
      • trimLeadingCharacter

        public static java.lang.String trimLeadingCharacter​(java.lang.String str,
                                                            char leadingCharacter)
        Trim all occurences of the supplied leading character from the given String.
        Parameters:
        str - the string to check
        leadingCharacter - the leading character to be trimmed
        Returns:
        the trimmed String
      • trimLeadingCharacter

        public static java.lang.String trimLeadingCharacter​(java.lang.String str,
                                                            CharacterChecker checker)
        Trim all occurences of the supplied leading character from the given String.
        Parameters:
        str - the string to check
        checker - the character checker
        Returns:
        the trimmed String
      • hasLength

        public static boolean hasLength​(java.lang.String string)
        Check that the given string param is neither null nor of length 0.
        Parameters:
        string - the string
        Returns:
        true if the String is not null and has length
      • parseLocaleString

        public static java.util.Locale parseLocaleString​(java.lang.String localeString)
        Parse the given localeString into a Locale. This is the inverse operation of Locale's toString.
        Parameters:
        localeString - the locale string
        Returns:
        a corresponding Locale instance