strsubst(X;Y;I;Z)

Returns the given string with a particular occurrence (or all occurrences) of a given substring replaced with a different substring.

Syntax

strsubst(X;Y;I;Z)
bstrsubst(X;Y;I;Z)

Input

Argument Type Description
X text The string on which to apply the function

A scalar value or the name of a column

Y text Y is the substring to be replaced in X.

A scalar value or the name of a column

I integer An index corresponding to the particular occurrence of Y to replace

The index I is 1-based (i.e., the first occurrence has index 1).

A negative index counts back from the end of the string (i.e., I=-1 refers to the last occurrence).

An index of 0 will cause all occurrences of the substring Y to be replaced.

Z text Z is the substring to replace Y in X.

A scalar value or the name of a column

Return Value

Returns a text value corresponding to the string X with the I'th occurrence (or all occurrences, if I=0) of the substring Y replaced by the substring Z.

If X is N/A, the result is N/A. If Y is N/A, the result is X unchanged. If Z is N/A, the substring is deleted (i.e., the empty string is substituted).

Sample Usage

value substring index newstring strsubst(value;substring;index;newstring)
'banana' 'an' 2 'dann' 'bandanna'
'banana' 'an' 0 'dann' 'bdanndanna'
'banana' 'an' 0 '' 'ba'
'banana' '' 2 'dann' 'banana'
'' 'an' 2 'dann'
'banana' 'an' -2 'dann' 'bdannana'

Additional Information

  • strsubst is Unicode (UTF-8) compliant and will work with Unicode or plain ASCII text fields.
  • If passed a string argument that is not legal Unicode, it will by default signal an error (configurable as a user preference).
  • A corresponding function bstrsubst can be used with non-Unicode strings (e.g., binary or legacy encodings).