Replace (String function): Difference between revisions
Jump to navigation
Jump to search
m (1 revision) |
m (1 revision) |
(No difference)
|
Revision as of 15:32, 19 January 2011
Replace occurrences of a substring with a substitute string (String class)
This intrinsic function replaces one or multiple occurrences of a substring of an
input string (the method object) with a substitute string,
and it returns the modified version of the input string.
The Replace function is available as of version 7.3 of the Sirius Mods.
Syntax
%outString = string:Replace( substring, replacement, [Count= number])
Syntax terms
%outStr | A string variable to receive the modified input string. |
---|---|
string | The string to which the method is applied. |
substring | The substring that the method attempts to replace in the input string. If string contains no occurrences of substring, %outStr is set to a copy of string. If substring is null or longer than 256 characters, the request is cancelled. |
replacement | A string that contains the characters that replace the specified substring. If replacement is *not specified, %outStr is set to a copy of string. *longer than 256 characters, the request is cancelled. *null, the substring characters are removed from string. |
Count=num | An optional, name-required, argument that is the number of occurrences of substring to replace. Only values greater than 0 are valid. If num is *less than the number of occurrences of the substring, only the first num occurrences are substituted. *greater than or equal to the number of occurrences of the substring, all occurrences of the given substring are substituted. *not specified, all occurrences are substituted. |
Usage notes
- The search for the target substring is context sensitive; case matters in the attempt to find, as well as in the replacement of, the target characters.
- Calling the Replace method with an empty replacement string argument is equivalent to using the Remove method.
Examples
The following request shows two examples of Replace calls against the same input string: the first call replaces two occurrences of a specified substring; the second removes all occurrences of a different substring.
begin %x is longstring %substr is string len 6 %repl is string len 2 %out is longstring
%x = 'How much wood would a woodchuck chuck' %substr = 'wo' %repl = '?' %out = %x:replace(%substr, %repl, Count=2) printText {~} is {%out}
%substr = ' wood' %repl = %out = %x:replace(%substr, %repl) printText {~} is {%out} %substr = ' wood' %repl = %out = %x:replace(%substr, %repl) printText {~} is {%out}
end
The request output follows:
%out is How much ?od ?uld a ?odchuck chuck %out is How much would achuck chuck