Replace (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
 
(28 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Template:String:Replace subtitle}}
{{Template:String:Replace subtitle}}
The <var>Replace</var> [[Intrinsic classes|intrinsic]] function replaces one, or multiple, occurrences of a substring within the method object input string with a substitute string, returning the modified version of the input string.


This [[Intrinsic classes|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==
==Syntax==
{{Template:String:Replace syntax}}
{{Template:String:Replace syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%outStr                                                                                            </th>
<tr><th>%outString</th>
<td>A string variable to receive the modified input string.                                           </td></tr>
<td>A string variable to receive the modified input <var class="term">string</var>.</td></tr>
<tr><th>string                                                                                             </th>
 
<td>The string to which the method is applied.                                                         </td></tr>
<tr><th>string</th>
<tr><th>substring                                                                                         </th>
<td>The <var class="term">string</var> to which the method is applied.</td></tr>
<td>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.                                   </td></tr>
 
<tr><th>replacement                                                                                       </th>
<tr><th>substring</th>
<td>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''.                                                                               </td></tr>
<td>The <var class="term">substring</var> that the method attempts to replace in the input <var class="term">string</var>. If <var class="term">string</var> contains no occurrences of <var class="term">substring</var>, <var class="term">%outstring</var> is set to a copy of <var class="term">string</var>. If <var class="term">substring</var> is null or longer than 255 characters, the request is cancelled.</td></tr>
<tr><th>Count=num </th>
 
<td>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.</td></tr>
<tr><th>replacement</th>
<td>A string that contains the characters that replace the specified <var class="term">substring</var>. If <var class="term">replacement</var> is:<ul><li><b><i>not specified</i></b>, <var class="term">%outstring</var> is set to a copy of <var class="term">string</var>;<li><b><i>longer than 255 characters</i></b>, the request is cancelled;<li><b><i>null</i></b>, the <var class="term">substring</var> characters are removed from <var class="term">string</var>.</ul></td></tr>
 
<tr><th><var>Count</var></th>
<td>An optional, but [[Notation conventions for methods#Named parameters|name required]], argument that is the number of occurrences of <var class="term">substring</var> to replace. Only values greater than 0 are valid. If <var class="term">count</var> is:
<ul>
<li><b><i>less</i></b> than the number of occurrences of the <var class="term">substring</var>, only the first <var class="term">count</var> occurrences are substituted.
<li><b><i>greater</i></b> than or <b><i>equal</i></b> to the number of occurrences of the <var class="term">substring</var>, all occurrences of the given <var class="term">substring</var> are substituted.
<li><b><i>not specified</i></b>, all occurrences are substituted.</ul></td></tr>
</table>
</table>


==Usage notes==
==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.
<ul><li>The search for the target <var class="term">substring</var> is context sensitive; <b><i>case matters</i></b> 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 (String function)|Remove]] method.
<li>Calling <var>Replace</var> with an empty replacement string argument is equivalent to using <var>[[Remove (String function)|Remove]]</var>.
<li><var>Replace</var> is available as of <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.3.</ul>


==Examples==
==Examples==
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 <var class="term">substring</var>; the second removes all occurrences of a different <var class="term">substring</var>.
<p class="code">begin
  %x is longstring
  %substr is string len 6
  %repl is string len 2
  %out is longstring


The following request shows two examples of Replace calls
  %x = 'How much wood would a woodchuck chuck'
against the same input string: the first call replaces two occurrences of a specified substring; the second removes all occurrences of a different substring.
  %substr = 'wo'
    begin
  %repl = '?'
    %x is longstring
  %out = %x:replace(%substr, %repl, Count=2)
    %substr is string len 6
  [[PrintText statement|printText]] {~} is {%out}
    %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)
    [[Intrinsic classes#printtext|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


  %substr = ' wood'
  %repl = <nowiki>''</nowiki>
  %out = %x:replace(%substr, %repl)
  printText {~} is {%out}
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>
The following example shows how to remove blanks:
<!--SOUL.DME.276.QA in QASHR-->
<p class="code">begin
%x is longstring
%x = 'a  spaced  out  woodchuck'
printText /{%x:replace(' ', '')}/
end
</p>
The result is:
<p class="output">/aspacedoutwoodchuck/</p>


==See also==
==See also==
[[List of intrinsic String methods]]
{{Template:String:Replace footer}}
 
[[Category:Intrinsic String methods|Replace function]]
[[Category:Intrinsic methods]]

Latest revision as of 19:19, 7 December 2018

Replace occurrences of a substring with a substitute string (String class)

The Replace intrinsic function replaces one, or multiple, occurrences of a substring within the method object input string with a substitute string, returning the modified version of the input string.

Syntax

%outString = string:Replace( substring, replacement, [Count= number])

Syntax terms

%outString 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, %outstring is set to a copy of string. If substring is null or longer than 255 characters, the request is cancelled.
replacement A string that contains the characters that replace the specified substring. If replacement is:
  • not specified, %outstring is set to a copy of string;
  • longer than 255 characters, the request is cancelled;
  • null, the substring characters are removed from string.
Count An optional, but name required, argument that is the number of occurrences of substring to replace. Only values greater than 0 are valid. If count is:
  • less than the number of occurrences of the substring, only the first count 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 Replace with an empty replacement string argument is equivalent to using Remove.
  • Replace is available as of Sirius Mods Version 7.3.

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} end

The request output follows:

%out is How much ?od ?uld a ?odchuck chuck %out is How much would achuck chuck

The following example shows how to remove blanks:

begin %x is longstring %x = 'a spaced out woodchuck' printText /{%x:replace(' ', )}/ end

The result is:

/aspacedoutwoodchuck/

See also