Difference between revisions of "Replace"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "<c>replace</c> Replace all matching elements of a list or map, or substrings of a string, with a new value. === Arguments === {| class="wikitable" |- ! Parameter Name !! De...")
 
(Formatting of Usage Notes)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<c>replace</c> Replace all matching elements of a list or map, or substrings of a string, with a new value.
+
<c>replace</c> replaces all matching elements of a list or map, or substrings of a string, with a new value.
  
  
Line 6: Line 6:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
! Parameter Name !! Default Value !! Meaning
+
! Parameter Name !! Type !! Meaning
 
|-
 
|-
 
| ''self'' || list, map or string || object to replace elements of
 
| ''self'' || list, map or string || object to replace elements of
Line 12: Line 12:
 
| ''oldval'' || any || value or substring to replace
 
| ''oldval'' || any || value or substring to replace
 
|-
 
|-
| ''newval'' || any || new value or substring to substitute where oldval
+
| ''newval'' || any || new value or substring to substitute where oldval is found
is found
+
|-
 
| ''maxcount'' || number, optional || if given replace no more than this many.
 
| ''maxcount'' || number, optional || if given replace no more than this many.
 
|-
 
|-
Line 21: Line 21:
 
=== Usage Notes ===
 
=== Usage Notes ===
 
Lists and maps are mutated in place, and return themselves.
 
Lists and maps are mutated in place, and return themselves.
Strings are immutable, so the original string is (of course) unchanged, but a new string with the replacement is returned.  Note that with maps, it is the values that are searched for and replaced, not the keys.
+
 
 +
Strings are immutable, so the original string is (of course) unchanged, but a new string with the replacement is returned.   
 +
 
 +
Note that with maps, it is the ''values'' that are searched for and replaced, not the keys.
  
 
== Example ==
 
== Example ==
  
 
<ms>"Happy Pappy".replace("app", "ol") // returns "Holy Poly"
 
<ms>"Happy Pappy".replace("app", "ol") // returns "Holy Poly"
[1,2,3,2,5].replace(2, 42) // returns (and mutates to) [2, 42, 3, 42, 5]
+
[1,2,3,2,5].replace(2, 42) // returns (and mutates to) [1, 42, 3, 42, 5]
 
d = {1: "one"}; d.replace("one", "ichi").  // returns (and mutates to) {1: "ichi"}</ms>
 
d = {1: "one"}; d.replace("one", "ichi").  // returns (and mutates to) {1: "ichi"}</ms>
  

Latest revision as of 21:49, 17 November 2023

replace replaces all matching elements of a list or map, or substrings of a string, with a new value.


Arguments

Parameter Name Type Meaning
self list, map or string object to replace elements of
oldval any value or substring to replace
newval any new value or substring to substitute where oldval is found
maxcount number, optional if given replace no more than this many.

Usage Notes

Lists and maps are mutated in place, and return themselves.

Strings are immutable, so the original string is (of course) unchanged, but a new string with the replacement is returned.

Note that with maps, it is the values that are searched for and replaced, not the keys.

Example

"Happy Pappy".replace("app", "ol")	// returns "Holy Poly"
[1,2,3,2,5].replace(2, 42)	// returns (and mutates to) [1, 42, 3, 42, 5]
d = {1: "one"}; d.replace("one", "ichi").  // returns (and mutates to) {1: "ichi"}