Package com.helger.base.text
Class TextVariableHelper
java.lang.Object
com.helger.base.text.TextVariableHelper
This class provides an easy way to replace variables in a string with other values. The variables
need to be present in the form of
${bla}. The variable helper supports masking with
the backslash (\) character so that the "$" can be represented as "\$".- Since:
- 10.2.0
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsVariables(@Nullable String sSourceString) Quickly check if a string contains a variable.static voidforEachTextAndVariable(@Nullable String sSourceString, @NonNull Consumer<String> aTextFragmentHandler, @NonNull Consumer<String> aVariableNameHandler) Parse the provided source string looking for variables in the form${...}and invoke callbacks for either text fragments or variable names.static @Nullable StringgetWithReplacedVariables(@Nullable String sSourceString, @NonNull UnaryOperator<String> aVariableProvider) Replace all variables in the form${...}in the provided source string using the provided variable provider.
-
Method Details
-
containsVariables
Quickly check if a string contains a variable.- Parameters:
sSourceString- the string to check for variables.- Returns:
trueif at least one variable is contained,falseif not.
-
forEachTextAndVariable
public static void forEachTextAndVariable(@Nullable String sSourceString, @NonNull Consumer<String> aTextFragmentHandler, @NonNull Consumer<String> aVariableNameHandler) Parse the provided source string looking for variables in the form${...}and invoke callbacks for either text fragments or variable names.- Parameters:
sSourceString- The source string to parse and analyze. May benull.aTextFragmentHandler- The callback to be invoked for each text fragment. May not benull.aVariableNameHandler- The callback to be invoked for each variable name. May not benull.
-
getWithReplacedVariables
public static @Nullable String getWithReplacedVariables(@Nullable String sSourceString, @NonNull UnaryOperator<String> aVariableProvider) Replace all variables in the form${...}in the provided source string using the provided variable provider.- Parameters:
sSourceString- The source string to parse and replace variables in. May benull.aVariableProvider- The function to resolve variable names to their values. May not benull.- Returns:
nullif the input string isnullor empty, otherwise the string with all variables replaced.
-