Class StringUtil


  • public class StringUtil
    extends Object
    A collection of utilities to manipulate strings.
    Author:
    wdeclerc
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • substitute

        public static String substitute​(String aString,
                                        Map<String,​String> variablesValues,
                                        String open,
                                        String close)
        Substitutes variables in aString. Variable names are delimited by open and close strings. The values are retrieved from the given map.
        Parameters:
        aString - the string on which to apply the substitution.
        variablesValues - a map containing the variable values. The keys are the variable names, the values are the variable values.
        open - the open delimiter for variables.
        close - the close delimiter for variables.
        Returns:
        the string with the substitution applied.
      • substitute

        public static String substitute​(String aString,
                                        Map<String,​String> variablesValues,
                                        String open,
                                        String close,
                                        int recursion)
        Substitutes variables in aString. Variable names are delimited by open and close strings. The values are retrieved from the given map.
        Parameters:
        aString - the string on which to apply the substitution.
        variablesValues - a map containg the variable values. The keys are the variable names, the values are the variable values.
        open - the open delimiter for variables.
        close - the close delimiter for variables.
        recursion - the number of recursion (internal counter to avoid endless loops)
        Returns:
        the string with the substitution applied.
      • substituteHex

        public static String substituteHex​(String aString)
        Substitutes hex values in aString and convert them to operating system char equivalents in the return string. Format is $[01] or $[6F,FF,00,1F] Example: "This is a hex encoded six digits number 123456 in this string: $[31,32,33,34,35,36]"
        Parameters:
        aString - the string on which to apply the substitution.
        Returns:
        the string with the substitution applied.
      • environmentSubstitute

        public static final String environmentSubstitute​(String aString,
                                                         Map<String,​String> systemProperties)
        Substitutes variables in aString with the environment values in the system properties
        Parameters:
        aString - the string on which to apply the substitution.
        systemProperties - the system properties to use
        Returns:
        the string with the substitution applied.
      • environmentSubstitute

        public static final String environmentSubstitute​(String aString,
                                                         Map<String,​String> systemProperties,
                                                         boolean escapeHexDelimiter)
        Substitutes variables in aString with the environment values in the system properties
        Parameters:
        aString - the string on which to apply the substitution.
        systemProperties - the system properties to use
        Returns:
        the string with the substitution applied.
      • substituteUnix

        public static String substituteUnix​(String aString,
                                            Map<String,​String> variables)
        Substitutes variables in aString. Variables are of the form "${}", following the Unix scripting convention. The values are retrieved from the given map.
        Parameters:
        aString - the string on which to apply the substitution.
        variables - a map containg the variable values. The keys are the variable names, the values are the variable values.
        Returns:
        the string with the substitution applied.
      • substituteWindows

        public static String substituteWindows​(String aString,
                                               Map<String,​String> variables)
        Substitutes variables in aString. Variables are of the form "%%%%", following the Windows convention. The values are retrieved from the given map.
        Parameters:
        aString - the string on which to apply the substitution.
        variables - a map containg the variable values. The keys are the variable names, the values are the variable values.
        Returns:
        the string with the substitution applied.
      • substituteField

        public static String substituteField​(String aString,
                                             RowMetaInterface rowMeta,
                                             Object[] rowData)
                                      throws KettleValueException
        Substitutes field values in aString. Field values are of the form "?{}". The values are retrieved from the specified row. Please note that the getString() method is used to convert to a String, for all values in the row.
        Parameters:
        aString - the string on which to apply the substitution.
        rowMeta - The row metadata to use.
        rowData - The row data to use
        Returns:
        the string with the substitution applied.
        Throws:
        KettleValueException - In case there is a String conversion error
      • getUsedVariables

        public static void getUsedVariables​(String aString,
                                            String open,
                                            String close,
                                            List<String> list,
                                            boolean includeSystemVariables)
        Search the string and report back on the variables used
        Parameters:
        aString - The string to search
        open - the open or "start of variable" characters ${ or %%
        close - the close or "end of variable" characters } or %%
        list - the list of variables to add to
        includeSystemVariables - also check for system variables.
      • isSystemVariable

        public static boolean isSystemVariable​(String aString)
      • getUsedVariables

        public static void getUsedVariables​(String aString,
                                            List<String> list,
                                            boolean includeSystemVariables)
      • generateRandomString

        public static final String generateRandomString​(int length,
                                                        String prefix,
                                                        String postfix,
                                                        boolean uppercase)
      • isEmpty

        public static final boolean isEmpty​(String string)
        Check if the string supplied is empty. A String is empty when it is null or when the length is 0
        Parameters:
        string - The string to check
        Returns:
        true if the string supplied is empty
      • isEmpty

        public static final boolean isEmpty​(StringBuilder string)
        Check if the StringBuilder supplied is empty. A StringBuilder is empty when it is null or when the length is 0
        Parameters:
        string - The StringBuilder to check
        Returns:
        true if the StringBuilder supplied is empty
      • getIndent

        public static String getIndent​(int indentLevel)
      • getFormattedDateTime

        public static String getFormattedDateTime​(Date date)
        Giving back a date/time string in the format following the rule from the most to the least significant
        Parameters:
        date - the date to convert
        Returns:
        a string in the form yyyddMM_hhmmss
      • getFormattedDateTime

        public static String getFormattedDateTime​(Date date,
                                                  boolean milliseconds)
        Giving back a date/time string in the format following the rule from the most to the least significant
        Parameters:
        date - the date to convert
        milliseconds - true when milliseconds should be added
        Returns:
        a string in the form yyyddMM_hhmmssSSS (milliseconds will be optional)
      • getFormattedDateTimeNow

        public static String getFormattedDateTimeNow()
        Giving back the actual time as a date/time string in the format following the rule from the most to the least significant
        Returns:
        a string in the form yyyddMM_hhmmss
      • getFormattedDateTimeNow

        public static String getFormattedDateTimeNow​(boolean milliseconds)
        Giving back the actual time as a date/time string in the format following the rule from the most to the least significant
        Parameters:
        milliseconds - true when milliseconds should be added
        Returns:
        a string in the form yyyddMM_hhmmssSSS (milliseconds will be optional)
      • IsInteger

        public static boolean IsInteger​(String str)
      • IsNumber

        public static boolean IsNumber​(String str)
      • IsDate

        public static boolean IsDate​(String str)
      • IsDate

        public static boolean IsDate​(String str,
                                     String mask)
      • getVariableName

        public static final String getVariableName​(String variable)
        remove specification from variable
        Parameters:
        variable - the variable to look for, with the $ or % variable specification.
        Returns:
        the variable name
      • isVariable

        public static boolean isVariable​(String variable)
        Parameters:
        variable - the variable to look for, with the $ or % variable specification.
        Returns:
        true if the input is a variable, false otherwise
      • hasVariable

        public static boolean hasVariable​(String variable)
        Parameters:
        variable - A String which may have a variable within it, with the $ or % variable specification.
        Returns:
        true if the input has a variable, false otherwise
      • safeToLowerCase

        public static String safeToLowerCase​(Object obj)
        Calls the String.toLowerCase() method on the String returned by a call to obj.toString(), guarding against NullPointerExceptions.
        Parameters:
        obj - the Object whose string representation is being turned to lower case
        Returns:
        a lower case String representation of the String returned by a call to obj .toString()
      • trimStart

        public static String trimStart​(String source,
                                       char c)
        Removes all instances of the specified character from the start of the given String.
        Parameters:
        source - the String to trim
        c - the character to remove from the String
        Returns:
        a new string with all instances of the specified character removed from the start
      • trimEnd

        public static String trimEnd​(String source,
                                     char c)
        Removes all instances of the specified character from the end of the given String.
        Parameters:
        source - the String to trim
        c - the character to remove from the String
        Returns:
        a new string with all instances of the specified character removed from the end
      • toUri

        public static URI toUri​(String filePathOrUrl)
        Convert a raw path such as c:\tmp\park.parquet and /tmp/file, or, URL such as pvfs://tkSnowflakeStaging/@ORC_FILES or hc://clusterName/path/to/file to a URI
        Parameters:
        filePathOrUrl -
        Returns: