Replace (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
Line 5: Line 5:
The Replace function is available as of version 7.3 of the [[Sirius Mods]].                             
The Replace function is available as of version 7.3 of the [[Sirius Mods]].                             
===Replace syntax===                                                                                   
===Replace syntax===                                                                                   
   %outStr = string:Replace( substring, replacement [, Count=num] )                                  
   %outStr = string:Replace( substring, replacement [, Count=num] )
                                                                                                     
 
===Syntax Terms===                                                                                     
===Syntax Terms===                                                                                     
<dl>                                                                                                   
<dl>                                                                                                   

Revision as of 02:30, 17 September 2010

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.

Replace syntax

  %outStr = string:Replace( substring, replacement [, Count=num] )

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