Class TextVariableHelper

java.lang.Object
com.helger.base.text.TextVariableHelper

@Immutable public final class TextVariableHelper extends Object
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 Details

    • containsVariables

      public static boolean containsVariables(@Nullable String sSourceString)
      Quickly check if a string contains a variable.
      Parameters:
      sSourceString - the string to check for variables.
      Returns:
      true if at least one variable is contained, false if 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 be null.
      aTextFragmentHandler - The callback to be invoked for each text fragment. May not be null.
      aVariableNameHandler - The callback to be invoked for each variable name. May not be null.
    • 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 be null.
      aVariableProvider - The function to resolve variable names to their values. May not be null.
      Returns:
      null if the input string is null or empty, otherwise the string with all variables replaced.