Package org.pentaho.di.core.util
Class StringUtil
- java.lang.Object
-
- org.pentaho.di.core.util.StringUtil
-
public class StringUtil extends Object
A collection of utilities to manipulate strings.- Author:
- wdeclerc
-
-
Field Summary
Fields Modifier and Type Field Description static String
CRLF
static String
EMPTY_STRING
static String
FIELD_CLOSE
static String
FIELD_OPEN
static String
HEX_CLOSE
static String
HEX_OPEN
static String
INDENTCHARS
static String[]
SYSTEM_PROPERTIES
static String
UNIX_CLOSE
static String
UNIX_OPEN
static String
WINDOWS_CLOSE
static String
WINDOWS_OPEN
-
Constructor Summary
Constructors Constructor Description StringUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
environmentSubstitute(String aString, Map<String,String> systemProperties)
Substitutes variables inaString
with the environment values in the system propertiesstatic String
environmentSubstitute(String aString, Map<String,String> systemProperties, boolean escapeHexDelimiter)
Substitutes variables inaString
with the environment values in the system propertiesstatic String
generateRandomString(int length, String prefix, String postfix, boolean uppercase)
static String
getFormattedDateTime(Date date)
Giving back a date/time string in the format following the rule from the most to the least significantstatic String
getFormattedDateTime(Date date, boolean milliseconds)
Giving back a date/time string in the format following the rule from the most to the least significantstatic String
getFormattedDateTimeNow()
Giving back the actual time as a date/time string in the format following the rule from the most to the least significantstatic 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 significantstatic String
getIndent(int indentLevel)
static void
getUsedVariables(String aString, String open, String close, List<String> list, boolean includeSystemVariables)
Search the string and report back on the variables usedstatic void
getUsedVariables(String aString, List<String> list, boolean includeSystemVariables)
static String
getVariableName(String variable)
remove specification from variablestatic boolean
hasVariable(String variable)
static String
initCap(String st)
static boolean
IsDate(String str)
static boolean
IsDate(String str, String mask)
static boolean
isEmpty(String string)
Check if the string supplied is empty.static boolean
isEmpty(StringBuilder string)
Check if the StringBuilder supplied is empty.static boolean
IsInteger(String str)
static boolean
IsNumber(String str)
static boolean
isSystemVariable(String aString)
static boolean
isVariable(String variable)
static String
safeToLowerCase(Object obj)
Calls theString.toLowerCase()
method on theString
returned by a call toobj.toString()
, guarding againstNullPointerException
s.static Date
str2dat(String arg0, String arg1, String val)
static double
str2num(String pattern, String decimal, String grouping, String currency, String value)
static String
substitute(String aString, Map<String,String> variablesValues, String open, String close)
Substitutes variables inaString
.static String
substitute(String aString, Map<String,String> variablesValues, String open, String close, int recursion)
Substitutes variables inaString
.static String
substituteField(String aString, RowMetaInterface rowMeta, Object[] rowData)
Substitutes field values inaString
.static String
substituteHex(String aString)
Substitutes hex values inaString
and convert them to operating system char equivalents in the return string.static String
substituteUnix(String aString, Map<String,String> variables)
Substitutes variables inaString
.static String
substituteWindows(String aString, Map<String,String> variables)
Substitutes variables inaString
.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 URIstatic String
trimEnd(String source, char c)
Removes all instances of the specified character from the end of the givenString
.static String
trimStart(String source, char c)
Removes all instances of the specified character from the start of the givenString
.
-
-
-
Field Detail
-
UNIX_OPEN
public static final String UNIX_OPEN
- See Also:
- Constant Field Values
-
UNIX_CLOSE
public static final String UNIX_CLOSE
- See Also:
- Constant Field Values
-
WINDOWS_OPEN
public static final String WINDOWS_OPEN
- See Also:
- Constant Field Values
-
WINDOWS_CLOSE
public static final String WINDOWS_CLOSE
- See Also:
- Constant Field Values
-
HEX_OPEN
public static final String HEX_OPEN
- See Also:
- Constant Field Values
-
HEX_CLOSE
public static final String HEX_CLOSE
- See Also:
- Constant Field Values
-
FIELD_OPEN
public static final String FIELD_OPEN
- See Also:
- Constant Field Values
-
FIELD_CLOSE
public static final String FIELD_CLOSE
- See Also:
- Constant Field Values
-
CRLF
public static final String CRLF
- See Also:
- Constant Field Values
-
INDENTCHARS
public static final String INDENTCHARS
- See Also:
- Constant Field Values
-
EMPTY_STRING
public static final String EMPTY_STRING
- See Also:
- Constant Field Values
-
SYSTEM_PROPERTIES
public static final String[] SYSTEM_PROPERTIES
-
-
Method Detail
-
substitute
public static String substitute(String aString, Map<String,String> variablesValues, String open, String close)
Substitutes variables inaString
. 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 inaString
. 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 inaString
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 inaString
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 inaString
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 inaString
. 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 inaString
. 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 inaString
. 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 searchopen
- the open or "start of variable" characters ${ or %%close
- the close or "end of variable" characters } or %%list
- the list of variables to add toincludeSystemVariables
- 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)
-
str2num
public static double str2num(String pattern, String decimal, String grouping, String currency, String value) throws KettleValueException
- Throws:
KettleValueException
-
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
-
str2dat
public static Date str2dat(String arg0, String arg1, String val) throws KettleValueException
- Throws:
KettleValueException
-
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 convertmilliseconds
- 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)
-
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 theString.toLowerCase()
method on theString
returned by a call toobj.toString()
, guarding againstNullPointerException
s.
-
trimStart
public static String trimStart(String source, char c)
Removes all instances of the specified character from the start of the givenString
.
-
trimEnd
public static String trimEnd(String source, char c)
Removes all instances of the specified character from the end of the givenString
.
-
-