Class StringUtil


  • public class StringUtil
    extends Object
    Various utility methods for working with strings.
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • trimToEmpty

        public static String trimToEmpty​(String string)
        Returns the trimmed (left and right) version of the input string. If null is passed, an empty string is returned.
        Parameters:
        string - the input string to trim
        Returns:
        the trimmed string, or an empty string if the input was null.
      • trimToNull

        public static String trimToNull​(String string)
        Returns the trimmed (left and right) form of the input string. If the string is empty after trimming (or null was passed in the first place), null is returned, i.e. the input string is reduced to nothing.
        Parameters:
        string - the string to trim
        Returns:
        the trimmed string or null
      • processMultiLineSQL

        public static String[] processMultiLineSQL​(String multiLineSQL,
                                                   boolean stripComments,
                                                   boolean splitStatements,
                                                   String endDelimiter)
        Removes any comments from multiple line SQL using stripComments(String, ChangeSet) and then extracts each individual statement using splitSQL(String, String, ChangeSet).
        Parameters:
        multiLineSQL - A String containing all the SQL statements
        stripComments - If true then comments will be stripped, if false then they will be left in the code
      • processMultiLineSQL

        public static String[] processMultiLineSQL​(String multiLineSQL,
                                                   boolean stripComments,
                                                   boolean splitStatements,
                                                   String endDelimiter,
                                                   ChangeSet changeSet)
        Removes any comments from multiple line SQL using stripComments(String, ChangeSet) and then extracts each individual statement using splitSQL(String, String, ChangeSet).
        Parameters:
        multiLineSQL - A String containing all the SQL statements
        stripComments - If true then comments will be stripped, if false then they will be left in the code
        changeSet - the changeset associated with the sql being parsed
      • isDelimiter

        protected static boolean isDelimiter​(String piece,
                                             String previousPiece,
                                             String endDelimiter)
        Returns true if the input is a delimiter in one of the popular RDBMSs. Recognized delimiters are: semicolon (;), a slash (as the only content) or the word GO (as the only content).
        Parameters:
        piece - the input line to test
        previousPiece - the characters in the input stream that came before piece
        endDelimiter - ??? (need to see this in a debugger to find out)
      • wrap

        @Deprecated
        public static String wrap​(String inputStr,
                                  int wrapPoint,
                                  int extraLinePadding)
        Deprecated.
        Liquibase does not wrap any console output, and instead lets the terminal handle its own wrapping. If you wish to use this method, consider whether its usage is truly necessary.
        Add new lines to the input string to cause output to wrap. Optional line padding can be passed in for the additional lines that are created
        Parameters:
        inputStr - The string to split and wrap
        wrapPoint - The point at which to split the lines
        extraLinePadding - Any additional spaces to add
        Returns:
        String Output string with new lines
      • splitSQL

        public static String[] splitSQL​(String multiLineSQL,
                                        String endDelimiter)
        Splits a candidate multi-line SQL statement along ;'s and "go"'s.
      • splitSQL

        public static String[] splitSQL​(String multiLineSQL,
                                        String endDelimiter,
                                        ChangeSet changeSet)
        Splits a candidate multi-line SQL statement along ;'s and "go"'s.
        Parameters:
        changeSet - the changeset associated with the sql being parsed
      • stripComments

        public static String stripComments​(String multiLineSQL)
        Searches through a String which contains SQL code and strips out any comments that are between \/**\/ or anything that matches SP--SP\n (to support the ANSI standard commenting of -- at the end of a line).
        Returns:
        The String without the comments in
      • stripComments

        public static String stripComments​(String multiLineSQL,
                                           ChangeSet changeSet)
        Searches through a String which contains SQL code and strips out any comments that are between \/**\/ or anything that matches SP--SP\n (to support the ANSI standard commenting of -- at the end of a line).
        Parameters:
        changeSet - the changeset associated with the sql being parsed
        Returns:
        The String without the comments in
      • repeat

        public static String repeat​(String string,
                                    int times)
      • join

        public static String join​(int[] array,
                                  String delimiter)
      • indent

        public static String indent​(String string,
                                    int padding)
      • lowerCaseFirst

        public static String lowerCaseFirst​(String string)
      • upperCaseFirst

        public static String upperCaseFirst​(String string)
      • hasUpperCase

        public static boolean hasUpperCase​(String string)
      • hasLowerCase

        public static boolean hasLowerCase​(String string)
      • standardizeLineEndings

        public static String standardizeLineEndings​(String string)
      • isAscii

        public static boolean isAscii​(String string)
      • isAscii

        public static boolean isAscii​(char ch)
        Returns true if ch is a "7-bit-clean" ASCII character (ordinal number < 128).
        Parameters:
        ch - the character to test
        Returns:
        true if 7 bit-clean, false otherwise.
      • escapeHtml

        public static String escapeHtml​(String str)
      • pad

        public static String pad​(String value,
                                 int length)
        Adds spaces to the right of the input value until the string has reached the given length. Nothing is done if the string already has the given length or if the string is even longer.
        Parameters:
        value - The string to pad (if necessary)
        length - the desired length
        Returns:
        the input string, padded if necessary.
      • leftPad

        public static String leftPad​(String value,
                                     int length)
        Adds spaces to the left of the input value until the string has reached the given length. Nothing is done if the string already has the given length or if the string is even longer.
        Parameters:
        value - The string to pad (if necessary)
        length - the desired length
        Returns:
        the input string, padded if necessary.
      • contains

        public static boolean contains​(String value,
                                       String containsValue)
        Returns true if the input string contains the specified value
        Parameters:
        value - String to be checked
        containsValue - String to look for
        Returns:
        true if String contains the value
      • isEmpty

        public static boolean isEmpty​(String value)
        Returns true if the input string is the empty string (null-safe).
        Parameters:
        value - String to be checked
        Returns:
        true if String is null or empty
      • isNotEmpty

        public static boolean isNotEmpty​(String value)
        Returns true if the input string is NOT the empty string. If the string is null, false is returned.
        Parameters:
        value - String to be checked
        Returns:
        true if string is not null and not empty (length > 0)
      • startsWith

        public static boolean startsWith​(String value,
                                         String startsWith)
        Checks whether the given value starts with the specified startsWith string.
        Parameters:
        value - the string to check
        startsWith - the prefix to check for
        Returns:
        true if value starts with startsWith, false otherwise. Returns false if either argument is null.
      • endsWith

        public static boolean endsWith​(String value,
                                       String endsWith)
        Checks whether the given value ends with the specified endsWith string.
        Parameters:
        value - the string to check
        endsWith - the prefix to check for
        Returns:
        true if value ends with endsWith, false otherwise. Returns false if either argument is null.
      • isWhitespace

        public static boolean isWhitespace​(CharSequence string)
        Returns true if the given string only consists of whitespace characters (null-safe)
        Parameters:
        string - the string to test
        Returns:
        true if the string is null or only consists of whitespaces.
      • isMinimumVersion

        public static boolean isMinimumVersion​(String minimumVersion,
                                               int candidateMajor,
                                               int candidateMinor,
                                               int candidatePatch)
        Compares a minimum version number given in string form (only the first three parts are considered) with a candidate version given as the three ints major, minor and patch.
        Parameters:
        minimumVersion - The minimum version that is required, given as a string with up to 3 parts, e.g. "7.4" or "9.6.3"
        candidateMajor - the version number to be tested, major part
        candidateMinor - the version number to be tested, minor part
        candidatePatch - the version number to be tested, patch part
        Returns:
        true if candidateMajor.candidateMinor.candidatePatch >= minimumVersion or false if not
      • limitSize

        public static String limitSize​(String string,
                                       int maxLength)
      • randomIdentifer

        public static String randomIdentifer​(int len)
        Produce a random identifer of the given length, consisting only of uppercase letters.
        Parameters:
        len - desired length of the string
        Returns:
        an identifier of the desired length
      • toKabobCase

        public static String toKabobCase​(String string)
        Converts a camelCase string to a kabob-case one
      • toCamelCase

        public static String toCamelCase​(String string)
        Converts a kabob-case or underscore_case string to a camel-case one
      • equalsIgnoreCaseAndEmpty

        public static boolean equalsIgnoreCaseAndEmpty​(String s1,
                                                       String s2)
        Returns if two strings are equal, ignoring:
        • case (uppercase/lowercase)
        • difference between null, and empty string, and a string that only has spaces
        Parameters:
        s1 - the first String to compare (or null)
        s2 - the second String to compare (or null)
        Returns:
        true if the Strings are equal by the above criteria, false in all other cases
      • trimRight

        public static String trimRight​(String string)
        Trims whitespace characters from the end of specified string
        Parameters:
        string - String to trim
        Returns:
        new String without the whitespace at the end
      • getLastBlockComment

        public static String getLastBlockComment​(String sqlString)
        Retrieves the last block comment in a SQL string, if any.
        Parameters:
        sqlString - the SQL string to search for the last block comment
        Returns:
        the last block comment in the SQL string, or null if none was found
      • getLastLineComment

        public static String getLastLineComment​(String sqlString)
        Returns the last line comment from a given SQL string, if there is one.
        Parameters:
        sqlString - the SQL string to search
        Returns:
        the last line comment from the SQL string, or null if there is no line comment
      • stripSqlCommentsAndWhitespacesFromTheEnd

        public static String stripSqlCommentsAndWhitespacesFromTheEnd​(String sqlString)
        Strips the comments and white spaces from the end of given SQL string.
        Parameters:
        sqlString - the SQL string to strip
        Returns:
        the stripped SQL string
      • concatConsistentCase

        public static String concatConsistentCase​(String baseString,
                                                  String addition)
        Concatenates the addition string to the baseString string, adjusting the case of "addition" to match the base string. If the string is all caps, append addition in all caps. If all lower case, append in all lower case. If baseString is mixed case, make no changes to addition.
      • stripEnclosingQuotes

        public static String stripEnclosingQuotes​(String string)
      • equalsWordNull

        public static boolean equalsWordNull​(String value)
        Check whether the value is 'null' (case insensitive)
      • splitCamelCase

        public static String[] splitCamelCase​(String str)

        Splits a camel-case string into words based on the came casing.

        This code originated from the StringUtils class of commons-lang

        Parameters:
        str - the String to split, may be null
        Returns:
        an array of parsed Strings, null if null String input
      • getBytesWithEncoding

        public static byte[] getBytesWithEncoding​(String string)
      • toLowerWithoutWhitespaces

        public static String toLowerWithoutWhitespaces​(String value)
        Parameters:
        value - string to process
        Returns:
        string without any whitespaces formatted to lowercase.
      • isNumeric

        public static boolean isNumeric​(CharSequence cs)

        Checks whether the char sequence is numeric by checking that all chars in the sequence are numbers, so (-1, 1.0 and 1F) will return false

        This code originated from the StringUtils class of commons-lang

        Parameters:
        cs - the arg to check if it is numeric
        Returns:
        true if convertible to numeric and false otherwise
      • isEmpty

        public static boolean isEmpty​(CharSequence cs)
      • splitToChunks

        public static List<String> splitToChunks​(String input,
                                                 int chunkSize)
        Split the input string into chunks no larger than the supplied chunkSize. If the string is shorter than the chunkSize, the resultant list will contain only a single entry.