sub

Description

The sub() function replaces the first occurrence of the pattern within the source string.

Syntax

result = sub (pattern, replacement, sourcestring);

Arguments

pattern Required. The regular expression pattern to search for.
replacement

Required. The replacement string.

sourcestring Required. The source string to search for the first occurrence of pattern.

Return Values

The resulting string

newstring = sub("\n$", "", textstring)

In this example, the first occurrence of a trailing new line is replaced with nothing, effectively chopping it off.

For more information, see gsub.