我还没弄清楚如何以更干净的方式分割字符串.
ref: copy/part (find line "#") -15
rest2: copy/part (skip (find line "#") 1) 450
Run Code Online (Sandbox Code Playgroud)
-15用于开始,450用于结束.
这不好,因为我投入了价值.
什么是正确的解决方案?
预先警告:在Rebol中有许多不同的方法可以实现这一目标.所以你可能会得到很多不同的建议.
首先,让我们坚持使用FIND的原始方法.
当您将FIND与系列一起使用时,您得到的是对基础系列数据的新视图,位于与系列数据开头不同的偏移处.
让我们从一些示例数据开始:
>> line: copy "this is foo#and here comes a long bar"
== "this is foo#and here comes a long bar"
Run Code Online (Sandbox Code Playgroud)
让我们找到#该行中的字符,并将结果称为POS:
>> pos: find line "#"
== "#and here comes a long bar"
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这基本上已经为您提供了拆分的第二部分(您称之为REST2).您只需跳过分隔符本身(然后复制结果字符串,使其独立于原始LINE字符串):
>> rest: copy next pos
== "and here comes a long bar"
Run Code Online (Sandbox Code Playgroud)
要提取初始部分,您可以使用COPY/part的一个很好的功能."/ part"改进(try help copy)的文档说:"对给定长度或位置的限制"(强调我的).我们已经把这个职位当作POS了.所以:
>> ref: copy/part line pos
== "this is foo"
Run Code Online (Sandbox Code Playgroud)
你去吧!完整的代码:
pos: find line "#"
ref: copy/part line pos
rest: copy next pos
Run Code Online (Sandbox Code Playgroud)
作为参考,这是一个基于PARSE的方法:
parse line [copy ref to "#" skip copy rest to end]
Run Code Online (Sandbox Code Playgroud)
我会在没有进一步解释的情况下让这个立场.如果你想了解更多有关PARSE的知识,那么一个好的起点是REBOL /核心用户指南(最初为REBOL 2.3编写)中的古老"解析"章节,但目前REBOL 2和3版本的基础知识基本相同).
最后一个辅助说明:"#"您可以使用#"#"在Rebol中编写的字符,而不是单项字符串.