Skip to main content

SWAP

Replaces all instances of a substring in a variable.

Synopsis

SWAP oldstring WITH newstring IN variable

Arguments

oldstring The substring to be replaced. An expression that resolves to a valid string or numeric.
newstring The replacement substring. An expression that resolves to a valid string or numeric. To delete oldstring, specify the empty string ("").
variable An existing variable containing a string value. variable may be a dynamic array. variable accepts a single dynamic array reference (A<i>), a single substring reference (A[s,l]), or a substring reference nested inside a dynamic array reference (A<i>[s,l]).

Description

The SWAP statement edits the value of variable by replacing all instances of oldstring with newstring. The oldstring and newstring values may be of different lengths. Matching of strings is case-sensitive.

The values of oldstring and newstring can be a string or a numeric. If numeric, the value is converted to canonical form (plus sign, leading and trailing zeros removed) before performing the string replacement.

To remove all instances of oldstring from variable, specify the null string ("") as the newstring value. The null string ("") cannot be used as the oldstring value.

Note:

Caché MVBasic supports the UniData SWAP statement for substring replacement. UniVerse implements a completely different SWAP statement for variable value exchange, which we do not support at this time. Caché MVBasic also supports the UniVerse CHANGE statement for substring replacement.

SWAP and CHANGE both perform string substitution, and are functionally identical. CONVERT performs character-for-character substitution.

Examples

The following example illustrates use of the SWAP statement, replacing a substring value in all the elements of a dynamic array:

cities="Pittsburg Penn.":@VM:"Philadephia Penn."
SWAP "Penn." WITH "PA" IN cities

See Also

FeedbackOpens in a new tab