Substitutes one string for another, within a string

(vl-string-subst new-str pattern string [start-pos])



The string to be substituted for pattern.


A string containing the pattern to be replaced.


The string to be searched for pattern.


An integer identifying the starting position of the search; 0 if omitted.

Note that the search is case-sensitive, and that vl-string-subst substitutes only the first occurrence it finds of the string.

Return Values

The value of string after any substitutions have been made.


Replace the string “Ben” with “Obi-wan”:

_$ (vl-string-subst "Obi-wan"
"Ben" "Ben Kenobi")
"Obi-wan Kenobi"

Replace “Ben” with “Obi-wan”:

_$ (vl-string-subst "Obi-wan"
"Ben" "ben Kenobi")
"ben Kenobi"

Nothing was substituted because vl-string-subst did not find a match for “Ben”; the “ben” in the string that was searched begins with a lowercase “b”.

Replace “Ben” with “Obi-wan”:

_$ (vl-string-subst "Obi-wan"
"Ben" "Ben Kenobi Ben")
"Obi-wan Kenobi Ben"

Note that there are two occurrences of “Ben” in the string that was searched, but vl-string-subst replaces only the first occurrence.

Replace “Ben” with “Obi-wan,” but start the search at the fourth character in the string:

_$ (vl-string-subst "Obi-wan"
"Ben" "Ben \"Ben\" Kenobi" 3)
"Ben \"Obi-wan\" Kenobi"

There are two occurrences of “Ben” in the string that was searched, but because vl-string-subst was instructed to begin searching at the fourth character, it found and replaced the second occurrence, not the first.