Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

Expand class method

Project: Environment Variables Unit

Unit: PJEnvVars

Class: TPJEnvironmentVars

Applies to: ~>3.0

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


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



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%.


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.

  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%'));

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