replace

Searches for a string within a string and deletes or replaces with an optional string.

Available in:

Apps (win) Apps (char) Reportwriter RPC Standalone PL
X X X X X

Syntax

string replace(str1,str2[,str3])
string         str1,str2,str3

Description

str1 specifies the string in which to search.
str2 specifies the search string.
str3 specifies the string with which to replace str2 if found in str1. If str3 is not specified, then all occurrences of str2 will be deleted from str1.

Notes

Example

Find and replace all occurrences of "San Jose" with "Los Gatos".
{
 char s[80], f[80], r[80];
 s = "San Jose is a lovely town. Do you know the way to San Jose?";
 f = "San Jose";
 r = "Los Gatos";
 
 printf(s);
 printf("Replace '"^^f^^"' with '"^^r^^"'");
 s = replace(s,f,r);
 printf(s);
 f = "the way to ";
 printf("Remove '"^^f^^"' from '"^^s^^"'");
 s = replace(s,f);
 printf(s);
}
returns
San Jose is a lovely town. Do you know the way to San Jose?
Replace 'San Jose' with 'Los Gatos'
Los Gatos is a lovely town. Do you know the way to Los Gatos?
Remove 'the way to ' from 'Los Gatos is a lovely town. Do you know the way to Los Gatos?'
Los Gatos is a lovely town. Do you know Los Gatos?