Class Strings2


  • public final class Strings2
    extends java.lang.Object

    String utilities not provided by Strings.

    Since:
    2.0
    Version:
    2.5
    Author:
    Michael Grove
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] base64Decode​(java.lang.String theStringToDecode)
      Base64 encodes the given byte array.
      static java.lang.String base64Encode​(byte[] theArrayToEncode)
      Base64 encodes the given byte array.
      static boolean endsWithIgnoreCase​(java.lang.String str, java.lang.String suffix)  
      static java.lang.String getRandomString​(int theLength)
      Return a string of random characters of the specified length.
      static java.lang.String getRandomString​(int theLength, java.util.Random random)  
      static java.lang.String hex​(byte[] theArray)
      Return the hex string value of the byte array
      static java.lang.String indentMultiline​(java.lang.String input, java.lang.String indentPrefix)
      Indent a multiline string by inserting the indentPrefix at the beginning of each line.
      static byte[] md5​(byte[] theBytes)
      Returns the md5 representation of a string
      static byte[] md5​(java.lang.String theString)
      Returns the md5 representation of a string
      static boolean startsWithIgnoreCase​(java.lang.String str, java.lang.String prefix)  
      static java.lang.String toTitleCase​(java.lang.String theStr)
      Convert a string to title case.
      static java.lang.String trimNewlines​(java.lang.String s)
      Remove leading and trailing spaces including newlines
      static java.lang.String urlDecode​(java.lang.String theString)
      URL decode the string using the UTF8 charset
      static java.lang.String urlDecode​(java.lang.String theString, java.nio.charset.Charset theCharset)
      URL decode the given string using the given Charset
      static java.lang.String urlEncode​(java.lang.String theString)
      URL encode the string using the UTF8 charset
      static java.lang.String urlEncode​(java.lang.String theString, java.nio.charset.Charset theCharset)
      URL encode the given string using the given Charset
      static java.lang.String withoutLastCharacter​(java.lang.String s)
      Returns the string that is one character shorter than the input string, without the last character.
      static java.lang.String withoutQuotes​(java.lang.String s)
      If the input string starts and ends with either double quotes or single quote, it is truncated, otherwise the input string is returned intact.
      static java.lang.String withoutTrailingSlash​(java.lang.String s)
      If the input string ends with slash character "/", it is truncated, otherwise the input string is returned intact.
      • Methods inherited from class java.lang.Object

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

      • toTitleCase

        public static java.lang.String toTitleCase​(java.lang.String theStr)
        Convert a string to title case. So if you have "this is a sentence" this function will return "This Is A Sentence"
        Parameters:
        theStr - the string to convert
        Returns:
        the converted string
      • hex

        public static java.lang.String hex​(byte[] theArray)
        Return the hex string value of the byte array
        Parameters:
        theArray - the input array
        Returns:
        the hex value of the bytes
      • md5

        public static byte[] md5​(java.lang.String theString)
        Returns the md5 representation of a string
        Parameters:
        theString - the string to md5
        Returns:
        the byte representation of the md5 sum of the string
      • md5

        public static byte[] md5​(byte[] theBytes)
        Returns the md5 representation of a string
        Parameters:
        theBytes - the bytes to md5
        Returns:
        the byte representation of the md5 sum of the string
      • getRandomString

        public static java.lang.String getRandomString​(int theLength)
        Return a string of random characters of the specified length.
        Parameters:
        theLength - the size of the random string to create
        Returns:
        a string of random letters
      • getRandomString

        public static java.lang.String getRandomString​(int theLength,
                                                       java.util.Random random)
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String theString)
        URL encode the string using the UTF8 charset
        Parameters:
        theString - the string to encode
        Returns:
        the url encoded string
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String theString,
                                                 java.nio.charset.Charset theCharset)
        URL encode the given string using the given Charset
        Parameters:
        theString - the string to encode
        theCharset - the charset to encode the string using
        Returns:
        the string encoded with the given charset
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String theString)
        URL decode the string using the UTF8 charset
        Parameters:
        theString - the string to decode
        Returns:
        the decoded string
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String theString,
                                                 java.nio.charset.Charset theCharset)
        URL decode the given string using the given Charset
        Parameters:
        theString - the string to decode
        theCharset - the charset to decode the string using
        Returns:
        the string decoded with the given charset
      • base64Encode

        public static java.lang.String base64Encode​(byte[] theArrayToEncode)
        Base64 encodes the given byte array. This utility is provided to abstract over the Sun implementation which is deprecated and marked for deletion. The implementation of this method will always work; so if the existing Sun implementation goes away, this will be switched to an appropriate implementation without requiring any changes to dependant code.
        Parameters:
        theArrayToEncode - the bytes to encode
        Returns:
        the bytes base64 encoded
      • base64Decode

        public static byte[] base64Decode​(java.lang.String theStringToDecode)
                                   throws java.io.IOException
        Base64 encodes the given byte array. This utility is provided to abstract over the Sun implementation which is deprecated and marked for deletion. The implementation of this method will always work; so if the existing Sun implementation goes away, this will be switched to an appropriate implementation without requiring any changes to dependant code.
        Parameters:
        theStringToDecode - the string to decode
        Returns:
        the bytes base64 decoded
        Throws:
        java.io.IOException - throw if there is an error while decoding
      • withoutLastCharacter

        public static java.lang.String withoutLastCharacter​(java.lang.String s)
        Returns the string that is one character shorter than the input string, without the last character. If empty String is given, IllegalArgumentException is thrown.
      • withoutTrailingSlash

        public static java.lang.String withoutTrailingSlash​(java.lang.String s)
        If the input string ends with slash character "/", it is truncated, otherwise the input string is returned intact.
      • indentMultiline

        public static java.lang.String indentMultiline​(java.lang.String input,
                                                       java.lang.String indentPrefix)
        Indent a multiline string by inserting the indentPrefix at the beginning of each line.
      • withoutQuotes

        public static java.lang.String withoutQuotes​(java.lang.String s)
        If the input string starts and ends with either double quotes or single quote, it is truncated, otherwise the input string is returned intact.
      • trimNewlines

        public static java.lang.String trimNewlines​(java.lang.String s)
        Remove leading and trailing spaces including newlines
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(java.lang.String str,
                                                   java.lang.String prefix)
        Parameters:
        str - a String
        prefix - a prefix
        Returns:
        true if str starts with prefix, ignoring case sensitivity
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(java.lang.String str,
                                                 java.lang.String suffix)
        Parameters:
        str - a String
        suffix - a suffix
        Returns:
        true if str ends with suffix, ignoring case sensitivity