Expand class method

Project: Environment Variables Unit

Unit: PJEnvVars

Class: TPJEnvironmentVars

Applies to: ~>3.0

class function Expand(const Str: string): string;

Description

Expands a string containing environment variables by replacing each environment variable name with its value.

Parameters:

Returns

If an environment variable does not exist then its name and enclosing % characters are left unmodified in the result string. The case of the environment variable name is not significant, i.e. %ENVVAR% is the same as %envvar%.

Example

The following code shows how Expand handles strings with zero, one and two valid environment variables along with how an undefined environment variable is handled.

begin
  WriteLn(./TPJEnvironmentVars.Expand('No variables')); // no variables
  TPJEnvironmentVars.SetValue('FOO', 'Lorem');   // defines FOO
  WriteLn(./TPJEnvironmentVars.Expand('One good variable: %FOO%'));
  TPJEnvironmentVars.SetValue('BAR', 'Ipsum');   // defines BAR
  WriteLn(./TPJEnvironmentVars.Expand('Two good variables: %FOO% & %BAR%'));
  TPJEnvironmentVars.Delete('FOO');              // FOO no longer defined
  WriteLn(./TPJEnvironmentVars.Expand('Good and bad variables: %FOO% & %BAR%'));
end;

When this code is run it writes the following:

No variables
One good variable: Lorem
Two good variables: Lorem & Ipsum
Good and bad variables: %FOO% & Ipsum