STRSUBST Function

Performs search and replace string substitution.

Usage

result = STRSUBST(expr, pattern, repl) 

Input Parameters

expr—The original scalar string expression.

pattern—Scalar string specifying regular expression for the search pattern.

repl—The replacement scalar string.

Returned Value

result—A scalar string containing the given substitutions.

Keywords

Global—If nonzero, causes the pattern to be replaced everywhere in the string with repl.

Discussion

STRSUBST uses regular expressions, not wildcard characters, for pattern matching. To use STRSUBST, it is crucial that you understand regular expressions. For a detailed discussion of regular expressions, see the chapter Working with Text in the PV‑WAVE Programmer’s Guide.

By default, only the first occurrence of pattern is replaced. Use the Global keyword to replace all occurrences of pattern.

Example 1

In this example, a list of .pro files is converted to .cpr files.

source = 'foo.pro, bar.pro'
; The letters "pro" are replaced by "cpr".
cprfiles = STRSUBST(source, 'pro', 'cpr', /Global)
PRINT, cprfiles
; PV-WAVE prints: foo.cpr, bar.cpr

Example 2

Suppose that you wish to change the file extension in the following string from .pp to .dat.

str1='$MDATA/ppt/thefile.pp'

You might try the following STRSUBST command:

str2=STRSUBST(str1, '.pp', '.dat')

Unfortunately, this command returns the following string, which is not the desired result:

PRINT, str2
$MYDATA/.datt/thefile.pp

The reason for this is that the pattern expression, '.pp', contains the regular expression special character . (dot), which means “match any single character except newline”. One way to obtain the correct result is to “escape” this special character with the backslash (\), as follows:

str2=STRSUBST(str1, '\\.pp', '.dat') 
PRINT, str2
$MYDATA/ppt/thefile.dat

Note:

Two backslashes are needed because in PV‑WAVE strings, you need a pair of backslashes to make a single backslash. For more information on this subject, see the chapter Working with Text in the PV‑WAVE Programmer’s Guide.

See Also

STRPUT, STRMATCH

For detailed information on regular expressions, see the PV‑WAVE Programmer’s Guide.