resolvePlaceholdersWithPlaceholder static method

String resolvePlaceholdersWithPlaceholder(
  1. String text,
  2. bool ignoreUnresolvablePlaceholders
)

Utility class for resolving #{...}-style placeholders within strings using system properties or environment variables.

This class provides static methods to:

  • Resolve system placeholders like #{user.name}
  • Configure whether unresolved placeholders should throw or be ignored

It supports default value resolution using the colon : separator, and escaping using the backslash \.

Example (strict resolution)

final result = SystemPropertyUtils.resolvePlaceholders('Hello #{USER}');
print(result); // Throws if USER is not found

Example (lenient resolution)

final result = SystemPropertyUtils.resolvePlaceholdersWithPlaceholder(
  'Hello #{USER:Guest}',
  true,
);
print(result); // Hello Guest

Resolves all #{...} placeholders in text using system properties and environment variables.

If ignoreUnresolvablePlaceholders is true, unresolved placeholders are left in place and no exception is thrown.

Example

final result = SystemPropertyUtils.resolvePlaceholdersWithPlaceholder(
  'Welcome #{USER:Guest}',
  true,
);
print(result); // e.g., 'Welcome Guest'

Implementation

static String resolvePlaceholdersWithPlaceholder(String text, bool ignoreUnresolvablePlaceholders) {
  if (text.isEmpty) {
    return text;
  }

  final helper = ignoreUnresolvablePlaceholders
      ? _nonStrictHelper
      : _strictHelper;

  return helper.replacePlaceholdersWithResolver(
    text,
    SystemPropertyPlaceholderResolver(text).resolvePlaceholder,
  );
}