Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    A collection of utility methods for String objects.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String AMP_ENCODE  
      static java.lang.String APOS_ENCODE  
      static java.lang.String GT_ENCODE  
      static char[] HEX_CHARS  
      static java.lang.String LT_ENCODE  
      static java.lang.String MD5  
      static java.lang.String PORTABLE_NEWLINE_REGEX  
      static java.lang.String QUOTE_ENCODE  
      static java.lang.String SHA1  
      static java.lang.String UNAMBIGUOUS_NUMBERS_AND_LETTERS_STRING
      24 upper case characters from the latin alphabet and numbers without '0' and 'O'.
      static java.lang.String USASCII
      Deprecated.
      use StandardCharsets.US_ASCII instead.
      static java.lang.String UTF8
      Deprecated.
      use StandardCharsets.UTF_8 instead.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.Appendable appendHeading​(java.lang.Appendable appendable, java.lang.String heading)  
      static java.lang.Appendable appendHeading​(java.lang.Appendable appendable, java.lang.String heading, char underlineChar)  
      static void appendTo​(java.util.Collection<? extends java.lang.Object> collection, java.lang.StringBuilder sb)  
      static void appendTo​(java.util.Collection<? extends java.lang.Object> collection, java.lang.String delimiter, java.lang.StringBuilder sb)  
      static <O> void appendTo​(java.util.Collection<O> collection, java.lang.StringBuilder sb, Consumer<O> appendFunction)  
      static <O> void appendTo​(java.util.Collection<O> collection, java.lang.String delimiter, java.lang.StringBuilder sb, Consumer<O> appendFunction)  
      static java.lang.String collectionToString​(java.util.Collection<? extends java.lang.Object> collection)
      Transform a collection of objects to a whitespace delimited String.
      static java.lang.String deleteXmlWhitespace​(java.lang.String string)  
      static java.lang.String encodeHex​(byte[] bytes)
      Encodes an array of bytes as String representation of hexadecimal.
      static java.lang.CharSequence escapeForXml​(java.lang.CharSequence input)
      Escape input for XML.
      static java.lang.CharSequence escapeForXmlAttribute​(java.lang.CharSequence input)
      Escape input for XML.
      static java.lang.CharSequence escapeForXmlAttributeApos​(java.lang.CharSequence input)
      Escape input for XML.
      static java.lang.CharSequence escapeForXmlText​(java.lang.CharSequence input)
      Escape input for XML.
      static java.lang.String hash​(java.lang.String data)
      Deprecated.
      use SHA1.hex(String) instead.
      static java.lang.String insecureRandomString​(int length)
      Returns a random String of numbers and letters (lower and upper case) of the specified length.
      static boolean isEmpty​(java.lang.CharSequence cs)
      Returns true if the given CharSequence is empty.
      static boolean isNotEmpty​(java.lang.CharSequence cs)
      Returns true if CharSequence is not null and is not empty, false otherwise.
      static boolean isNotEmpty​(java.lang.CharSequence... css)
      Returns true if all given CharSequences are not empty.
      static boolean isNullOrEmpty​(java.lang.CharSequence cs)
      Returns true if the given CharSequence is null or empty.
      static boolean isNullOrEmpty​(java.lang.CharSequence... css)
      Returns true if all given CharSequences are either null or empty.
      static boolean isNullOrNotEmpty​(java.lang.CharSequence cs)  
      static java.lang.String maybeToString​(java.lang.CharSequence cs)
      Return the String representation of the given char sequence if it is not null.
      static int nullSafeCharSequenceComparator​(java.lang.CharSequence csOne, java.lang.CharSequence csTwo)  
      static boolean nullSafeCharSequenceEquals​(java.lang.CharSequence csOne, java.lang.CharSequence csTwo)  
      static java.lang.String randomString​(int length)  
      static java.lang.String randomString​(int length, java.util.Random random)  
      static <CS extends java.lang.CharSequence>
      CS
      requireNotNullNorEmpty​(CS cs, java.lang.String message)
      Require a CharSequence to be neither null, nor empty.
      static <CS extends java.lang.CharSequence>
      CS
      requireNotNullOrEmpty​(CS cs, java.lang.String message)
      static <CS extends java.lang.CharSequence>
      CS
      requireNullOrNotEmpty​(CS cs, java.lang.String message)  
      static java.lang.String returnIfNotEmptyTrimmed​(java.lang.String string)  
      static java.lang.String secureOfflineAttackSafeRandomString()
      Generate a secure random string with is human readable.
      static java.lang.String secureOnlineAttackSafeRandomString()  
      static java.lang.String secureUniqueRandomString()  
      static java.util.List<java.lang.String> splitLinesPortable​(java.lang.String input)  
      static java.lang.StringBuilder toStringBuilder​(java.util.Collection<? extends java.lang.Object> collection, java.lang.String delimiter)
      Transform a collection of objects to a delimited String.
      static java.util.List<java.lang.String> toStrings​(java.util.Collection<? extends java.lang.CharSequence> charSequences)  
      static byte[] toUtf8Bytes​(java.lang.String string)  
      • Methods inherited from class java.lang.Object

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

      • escapeForXml

        public static java.lang.CharSequence escapeForXml​(java.lang.CharSequence input)
        Escape input for XML.
        Parameters:
        input - the input to escape.
        Returns:
        the XML escaped variant of input.
      • escapeForXmlAttribute

        public static java.lang.CharSequence escapeForXmlAttribute​(java.lang.CharSequence input)
        Escape input for XML.
        Parameters:
        input - the input to escape.
        Returns:
        the XML escaped variant of input.
        Since:
        4.2
      • escapeForXmlAttributeApos

        public static java.lang.CharSequence escapeForXmlAttributeApos​(java.lang.CharSequence input)
        Escape input for XML.

        This is an optimized variant of escapeForXmlAttribute(CharSequence) for XML where the XML attribute is quoted using ''' (Apos).

        Parameters:
        input - the input to escape.
        Returns:
        the XML escaped variant of input.
        Since:
        4.2
      • escapeForXmlText

        public static java.lang.CharSequence escapeForXmlText​(java.lang.CharSequence input)
        Escape input for XML.
        Parameters:
        input - the input to escape.
        Returns:
        the XML escaped variant of input.
        Since:
        4.2
      • hash

        @Deprecated
        public static java.lang.String hash​(java.lang.String data)
        Deprecated.
        use SHA1.hex(String) instead.
        Hashes a String using the SHA-1 algorithm and returns the result as a String of hexadecimal numbers. This method is synchronized to avoid excessive MessageDigest object creation. If calling this method becomes a bottleneck in your code, you may wish to maintain a pool of MessageDigest objects instead of using this method.

        A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.

        Parameters:
        data - the String to compute the hash of.
        Returns:
        a hashed version of the passed-in String
      • encodeHex

        public static java.lang.String encodeHex​(byte[] bytes)
        Encodes an array of bytes as String representation of hexadecimal.
        Parameters:
        bytes - an array of bytes to convert to a hex string.
        Returns:
        generated hex string.
      • toUtf8Bytes

        public static byte[] toUtf8Bytes​(java.lang.String string)
      • insecureRandomString

        public static java.lang.String insecureRandomString​(int length)
        Returns a random String of numbers and letters (lower and upper case) of the specified length. The method uses the Random class that is built-in to Java which is suitable for low to medium grade security uses. This means that the output is only pseudo random, i.e., each number is mathematically generated so is not truly random.

        The specified length must be at least one. If not, the method will return null.

        Parameters:
        length - the desired length of the random String to return.
        Returns:
        a random String of numbers and letters of the specified length.
      • secureOfflineAttackSafeRandomString

        public static java.lang.String secureOfflineAttackSafeRandomString()
        Generate a secure random string with is human readable. The resulting string consists of 24 upper case characters from the Latin alphabet and numbers without '0' and 'O', grouped into 4-characters chunks, e.g. "TWNK-KD5Y-MT3T-E1GS-DRDB-KVTW". The characters are randomly selected by a cryptographically secure pseudorandom number generator (CSPRNG).

        The string can be used a backup "code" for secrets, and is in fact the same as the one backup code specified in XEP-0373 and the one used by the Backup Format v2 of OpenKeychain.

        Returns:
        a human readable secure random string.
        See Also:
        XEP-0373 ยง5.4 Encrypting the Secret Key Backup
      • randomString

        public static java.lang.String randomString​(int length)
      • randomString

        public static java.lang.String randomString​(int length,
                                                    java.util.Random random)
      • isNotEmpty

        public static boolean isNotEmpty​(java.lang.CharSequence cs)
        Returns true if CharSequence is not null and is not empty, false otherwise. Examples: isNotEmpty(null) - false isNotEmpty("") - false isNotEmpty(" ") - true isNotEmpty("empty") - true
        Parameters:
        cs - checked CharSequence
        Returns:
        true if string is not null and is not empty, false otherwise
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.CharSequence cs)
        Returns true if the given CharSequence is null or empty.
        Parameters:
        cs - TODO javadoc me please
        Returns:
        true if the given CharSequence is null or empty
      • isNotEmpty

        public static boolean isNotEmpty​(java.lang.CharSequence... css)
        Returns true if all given CharSequences are not empty.
        Parameters:
        css - the CharSequences to test.
        Returns:
        true if all given CharSequences are not empty.
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.CharSequence... css)
        Returns true if all given CharSequences are either null or empty.
        Parameters:
        css - the CharSequences to test.
        Returns:
        true if all given CharSequences are null or empty.
      • isNullOrNotEmpty

        public static boolean isNullOrNotEmpty​(java.lang.CharSequence cs)
      • isEmpty

        public static boolean isEmpty​(java.lang.CharSequence cs)
        Returns true if the given CharSequence is empty.
        Parameters:
        cs - TODO javadoc me please
        Returns:
        true if the given CharSequence is empty
      • collectionToString

        public static java.lang.String collectionToString​(java.util.Collection<? extends java.lang.Object> collection)
        Transform a collection of objects to a whitespace delimited String.
        Parameters:
        collection - the collection to transform.
        Returns:
        a String with all the elements of the collection.
      • toStringBuilder

        public static java.lang.StringBuilder toStringBuilder​(java.util.Collection<? extends java.lang.Object> collection,
                                                              java.lang.String delimiter)
        Transform a collection of objects to a delimited String.
        Parameters:
        collection - the collection to transform.
        delimiter - the delimiter used to delimit the Strings.
        Returns:
        a StringBuilder with all the elements of the collection.
      • appendTo

        public static void appendTo​(java.util.Collection<? extends java.lang.Object> collection,
                                    java.lang.StringBuilder sb)
      • appendTo

        public static <O> void appendTo​(java.util.Collection<O> collection,
                                        java.lang.StringBuilder sb,
                                        Consumer<O> appendFunction)
      • appendTo

        public static void appendTo​(java.util.Collection<? extends java.lang.Object> collection,
                                    java.lang.String delimiter,
                                    java.lang.StringBuilder sb)
      • appendTo

        public static <O> void appendTo​(java.util.Collection<O> collection,
                                        java.lang.String delimiter,
                                        java.lang.StringBuilder sb,
                                        Consumer<O> appendFunction)
      • nullSafeCharSequenceEquals

        public static boolean nullSafeCharSequenceEquals​(java.lang.CharSequence csOne,
                                                         java.lang.CharSequence csTwo)
      • requireNotNullOrEmpty

        @Deprecated
        public static <CS extends java.lang.CharSequence> CS requireNotNullOrEmpty​(CS cs,
                                                                                   java.lang.String message)
        Require a CharSequence to be neither null, nor empty.
        Type Parameters:
        CS - CharSequence type
        Parameters:
        cs - CharSequence
        message - error message
        Returns:
        cs TODO javadoc me please
      • requireNotNullNorEmpty

        public static <CS extends java.lang.CharSequence> CS requireNotNullNorEmpty​(CS cs,
                                                                                    java.lang.String message)
        Require a CharSequence to be neither null, nor empty.
        Type Parameters:
        CS - CharSequence type
        Parameters:
        cs - CharSequence
        message - error message
        Returns:
        cs TODO javadoc me please
      • requireNullOrNotEmpty

        public static <CS extends java.lang.CharSequence> CS requireNullOrNotEmpty​(CS cs,
                                                                                   java.lang.String message)
      • maybeToString

        public static java.lang.String maybeToString​(java.lang.CharSequence cs)
        Return the String representation of the given char sequence if it is not null.
        Parameters:
        cs - the char sequence or null.
        Returns:
        the String representation of cs or null.
      • deleteXmlWhitespace

        public static java.lang.String deleteXmlWhitespace​(java.lang.String string)
      • appendHeading

        public static java.lang.Appendable appendHeading​(java.lang.Appendable appendable,
                                                         java.lang.String heading)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • appendHeading

        public static java.lang.Appendable appendHeading​(java.lang.Appendable appendable,
                                                         java.lang.String heading,
                                                         char underlineChar)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • splitLinesPortable

        public static java.util.List<java.lang.String> splitLinesPortable​(java.lang.String input)
      • toStrings

        public static java.util.List<java.lang.String> toStrings​(java.util.Collection<? extends java.lang.CharSequence> charSequences)