`split` 函数的逆函数:`join` 使用定界符的字符串
Inverse of `split` function: `join` a string using a delimeter
在 Red and Rebol(3) 中,您可以使用 split
函数将字符串拆分为项目列表:
>> items: split {1, 2, 3, 4} {,}
== ["1" " 2" " 3" " 4"]
join
将项目列表转换为字符串对应的反函数是什么?它的工作方式应类似于以下内容:
>> join items {, }
== "1, 2, 3, 4"
rejoin 有一个旧的修改
rejoin: func [
"Reduces and joins a block of values - allows /with refinement."
block [block!] "Values to reduce and join"
/with join-thing "Value to place in between each element"
][
block: reduce block
if with [
while [not tail? block: next block][
insert block join-thing
block: next block
]
block: head block
]
append either series? first block [
copy first block
] [
form first block
]
next block
]
这样称呼它 rejoin/with [..] 分隔符
但我很确定,还有其他甚至更老的解决方案。
暂无inbuild功能,需要自己实现:
>> join: function [series delimiter][length: either char? delimiter [1][length? delimiter] out: collect/into [foreach value series [keep rejoin [value delimiter]]] copy {} remove/part skip tail out negate length length out]
== func [series delimiter /local length out value][length: either char? delimiter [1] [length? delimiter] out: collect/into [foreach value series [keep rejoin [value delimiter]]] copy "" remove/part skip tail out negate length length out]
>> join [1 2 3] #","
== "1,2,3"
>> join [1 2 3] {, }
== "1, 2, 3"
根据请求,这里是拆分成更多行的函数:
join: function [
series
delimiter
][
length: either char? delimiter [1][length? delimiter]
out: collect/into [
foreach value series [keep rejoin [value delimiter]]
] copy {}
remove/part skip tail out negate length length
out
]
以下函数有效:
myjoin: function[blk[block!] delim [string!]][
outstr: ""
repeat i ((length? blk) - 1)[
append outstr blk/1
append outstr delim
blk: next blk ]
append outstr blk ]
probe myjoin ["A" "B" "C" "D" "E"] ", "
输出:
"A, B, C, D, E"
在 Red and Rebol(3) 中,您可以使用 split
函数将字符串拆分为项目列表:
>> items: split {1, 2, 3, 4} {,}
== ["1" " 2" " 3" " 4"]
join
将项目列表转换为字符串对应的反函数是什么?它的工作方式应类似于以下内容:
>> join items {, }
== "1, 2, 3, 4"
rejoin 有一个旧的修改
rejoin: func [
"Reduces and joins a block of values - allows /with refinement."
block [block!] "Values to reduce and join"
/with join-thing "Value to place in between each element"
][
block: reduce block
if with [
while [not tail? block: next block][
insert block join-thing
block: next block
]
block: head block
]
append either series? first block [
copy first block
] [
form first block
]
next block
]
这样称呼它 rejoin/with [..] 分隔符
但我很确定,还有其他甚至更老的解决方案。
暂无inbuild功能,需要自己实现:
>> join: function [series delimiter][length: either char? delimiter [1][length? delimiter] out: collect/into [foreach value series [keep rejoin [value delimiter]]] copy {} remove/part skip tail out negate length length out]
== func [series delimiter /local length out value][length: either char? delimiter [1] [length? delimiter] out: collect/into [foreach value series [keep rejoin [value delimiter]]] copy "" remove/part skip tail out negate length length out]
>> join [1 2 3] #","
== "1,2,3"
>> join [1 2 3] {, }
== "1, 2, 3"
根据请求,这里是拆分成更多行的函数:
join: function [
series
delimiter
][
length: either char? delimiter [1][length? delimiter]
out: collect/into [
foreach value series [keep rejoin [value delimiter]]
] copy {}
remove/part skip tail out negate length length
out
]
以下函数有效:
myjoin: function[blk[block!] delim [string!]][
outstr: ""
repeat i ((length? blk) - 1)[
append outstr blk/1
append outstr delim
blk: next blk ]
append outstr blk ]
probe myjoin ["A" "B" "C" "D" "E"] ", "
输出:
"A, B, C, D, E"