StringReplace() Function
- Last UpdatedFeb 21, 2017
- 2 minute read
Searches for a string within another string and, if found, replaces it with yet another string. You can specify:
-
Case-sensitivity - This determines if uppercase letters and lowercase letters are to be treated as identical letters or not.
-
Number of occurrences to replace - This is useful if more than one occurrence of the search string is found.
-
Match whole words - Use this if the search string is a whole word.
Note: This function does not support double byte character sets.
Syntax
result = StringReplace (string, searchfor, replacewith, casesens, numtoreplace, matchwholewords)
Parameters
string
The string to search within. A literal string, message tagname, or string expression.
searchfor
The string that is to be searched for. A literal string, message tagname, or string expression.
replacewith
The string that is used as replacement. A literal string, message tagname, or string expression.
casesens
Determines whether the search is case sensitive. Can be 0 or 1, discrete tagname or Boolean expression.
0 - search is not case sensitive (uppercase and lowercase are considered the same)
1 - search is case sensitive (uppercase and lowercase are considered to be different)
numtoreplace
The number of replacements to make. Set it to -1 to replace all occurrences of the found search string. A literal integer value, integer tagname, or integer expression.
matchwholewords
Determines whether only whole words are matched. Can be 0 or 1, discrete tagname, or Boolean expression.
0 - the function looks for the search string characters anywhere in the string
1 - only whole words are matched
Example(s)
This statement replaces only the first occurrence and returns "MTY 030 MTX".
StringReplace("MTX 030 MTX","MTX","MTY",0,1,0)
This statement replaces all occurrences and returns "MTY 030 MTY".
StringReplace("MTX 030 MTX","MTX","MTY",0,-1,0)
This statement replaces all occurrences that match the case and returns "MTY 030 mtx".
StringReplace("MTX 030 mtx","MTX","MTY",1,-1,0)
This statement replaces all occurrences that are whole words and returns "MTY 030 QMTX".
StringReplace("MTX 030 QMTX","MTX","MTY",0,-1,1)