REPLACE statement

Purpose

Within a specified string, replace all occurrences of one string with another string.

Syntax

REPLACE [ANY] MatchString WITH NewString IN MainString

Remarks

The REPLACE statement replaces all occurrences of MatchString in MainString with NewString.  The replacement can cause MainString to grow or shrink in size.  MainString must be a string variable; MatchString and NewString may be string expressions.  REPLACE is case-sensitive. When a match is found, the scan for the next match begins at the position immediately following the prior match.

ANY

If you use the ANY option, within MainString, each occurrence of each character in MatchString will be replaced with the corresponding character in NewString. In this case, MatchString and NewString must be the same length, because there is a one-to-one correspondence between their characters.

See also

EXTRACT$, INSTR, LTRIM$, MID$, REMOVE$, RETAIN$, RIGHT$, RTRIM$, TALLY, TRIM$, VERIFY

Example

A$ = "abacadabra"

'now replace "bac" with "----bac----"

REPLACE "bac" WITH "----bac----" IN A$

 

A$ = "abacadabra"

'now replace all "b", "a", and "c" with "*"

REPLACE ANY "bac" WITH "***" IN A$