< Rebol Programming
USAGE:
REPLACE target search replace /all /case /tail
DESCRIPTION:
Replaces the search value with the replace value within the target series.
REPLACE is a function value.
ARGUMENTS
- target -- Series that is being modified (Type: series)
- search -- Value to be replaced (Type: any)
- replace -- Value to replace with (will be called each time if a function) (Type: any)
REFINEMENTS
- /all -- Replace all occurrences
- /case -- Case-sensitive replacement
- /tail -- Return target after the last replacement position
SOURCE CODE
replace: func [
{Replaces the search value with the replace value within the target series.}
target [series!] "Series that is being modified"
search "Value to be replaced"
replace {Value to replace with (will be called each time if a function)}
/all "Replace all occurrences"
/case "Case-sensitive replacement"
/tail "Return target after the last replacement position"
/local save-target len value pos do-break
][
save-target: target
len: system/words/case [
bitset? :search 1
any-string? target [
if any [not any-string? :search tag? :search] [search: form :search]
length? :search
]
any-block? :search [length? :search]
true 1
]
do-break: unless all [:break]
while pick [
[pos: find target :search]
[pos: find/case target :search]
] not case [
(value: replace pos)
target: change/part pos :value len
do-break
]
either tail [target] [save-target]
]
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.