Replace (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{Template:String:Replace subtitle}}
This [[Intrinsic classes|intrinsic]] function replaces one or multiple occurrences of a substring of an
This [[Intrinsic classes|intrinsic]] function replaces one or multiple occurrences of a substring of an
input string (the method object) with a substitute string,                                             
input string (the method object) with a substitute string,                                             
Line 4: Line 6:
                                                                                                        
                                                                                                        
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===                                                                                  
==Syntax==
  %outStr = string:Replace( substring, replacement [, Count=num] )
{{Template:String:Replace syntax}}
 
===Syntax terms===
===Syntax Terms===                                                                                    
<dl>                                                                                                   
<dl>                                                                                                   
<dt>%outStr                                                                                             
<dt>%outStr                                                                                             
Line 33: Line 34:
                                                                                    
                                                                                    
</dl>                                                                               
</dl>                                                                               
===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.                                                       
*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 (String function)|Remove]] method.
*Calling the Replace method with an empty replacement string argument is equivalent to using the [[Remove (String function)|Remove]] method.


===Examples===                                                                   
==Examples==
                                                                                    
                                                                                    
The following request shows two examples of Replace calls                           
The following request shows two examples of Replace calls                           

Revision as of 05:14, 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

See also

List of intrinsic String methods