Rebol 程式設計/替換
外觀
REPLACE target search replace /all /case /tail
在目標序列中用替換值替換搜尋值。
REPLACE 是一個函式值。
- 目標 -- 被修改的序列(型別:序列)
- 搜尋 -- 要替換的值(型別:任何)
- 替換 -- 用它替換的值(如果是一個函式,每次都會被呼叫)(型別:任何)
- /all -- 替換所有出現
- /case -- 區分大小寫的替換
- /tail -- 返回最後一個替換位置後的目標
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]
]