Requires: gfawinx.lg32
Returns a string in which a specified substring has been replaced with another substring a specified number of times.
str = Replace(src, find, replaceby [, start] [, count] [, compare])
str, find, replaceby | : string expression |
src | : string variable |
start, count, compare | : integer expression |
The src argument should be a string variable containing the string to be found that you wish to replace, find is the substring being searched for and replaceby the string that is inserted in its place. The optional start argument specifies the position within src where the search is to begin; if it is omitted, 1 or the first character of the string is assumed. The optional count argument specifies the number of substring substitutions to perform; if this is omitted, the default value is -1, which tells the function to make all possible substitutions. Finally, the optional compare is a numeric value indicating the kind of comparison to use when evaluating substrings and if this is omitted, the default value is 0 is assumed, which instructs the function to perform a binary comparison - the number for this last argument can be any value from Mode Compare.
The return value of the Replace function is a string in which the substitutions have been made.
$Library "gfawinx"
Dim txt As String = "GFABasic32GFABasic32"
' Case insensitive replacement
Debug Replace(txt, "a", "xx", 4, 2, 1)
Debug.Show
The general instruction passed to the Replace function is to change all lower-case 'a' characters to 'xx'; however, this operation is expanded by the final compare argument of 1 which specifies that the search should be case INsensitive so all upper-case 'A' characters are to be replaced as well. Without any further arguments, this would result in four substitutions; however, the inclusion of the value 4 in the start argument means the first 'A' is excluded from the replacement process and the passing of the value 2 in the count argument means that only two substitutions are made, resulting in the final 'a' remaining unconverted. Hence, the output of this example is GFABxxsic32GFxxBasic32.
Replace is defined using FunctionVar because this type takes implicit ByRef parameters (each parameter without an explicit ByVal is implicit ByRef). Consequently, when a literal string is passed – like “a” and “xx” - the compiler inserts code to copy the literal strings in hidden local variables that are then passed by reference. However, if the parameter is a string variable the variable is passed by reference without first making a copy.
{Created by Sjouke Hamstra; Last updated: 08/08/2019 by James Gaite}