Replace (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 30: Line 30:
The following request shows two examples of <var>Replace</var> calls
The following request shows two examples of <var>Replace</var> 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.
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
<p class="code">begin
    %x is longstring
%x is longstring
    %substr is string len 6
%substr is string len 6
    %repl is string len 2
%repl is string len 2
    %out is longstring
%out is longstring


    %x = 'How much wood would a woodchuck chuck'
%x = 'How much wood would a woodchuck chuck'
    %substr = 'wo'
%substr = 'wo'
    %repl = '?'
%repl = '?'
    %out = %x:replace(%substr, %repl, Count=2)
%out = %x:replace(%substr, %repl, Count=2)
    [[Intrinsic classes#printtext|printText]] {~} is {%out}
[[Intrinsic classes#printtext|printText]] {~} is {%out}


    %substr = ' wood'
%substr = ' wood'
    %repl = ''
%repl = ''
    %out = %x:replace(%substr, %repl)
%out = %x:replace(%substr, %repl)
    printText {~} is {%out}
printText {~} is {%out}
    %substr = ' wood'
%substr = ' wood'
    %repl = ''
%repl = ''
    %out = %x:replace(%substr, %repl)
%out = %x:replace(%substr, %repl)
    printText {~} is {%out}
printText {~} is {%out}


    end
end


</p>
The request output follows:
The request output follows:
    %out is How much ?od ?uld a ?odchuck chuck
<p class="output">%out is How much ?od ?uld a ?odchuck chuck
    %out is How much would achuck chuck
%out is How much would achuck chuck


</p>
==See also==
==See also==
[[List of intrinsic String methods]]
[[List of intrinsic String methods]]

Revision as of 15:49, 20 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

See also

List of intrinsic String methods