【问题标题】:Can rebol trim remove blank lines without removing CRLF?rebol trim 可以在不删除 CRLF 的情况下删除空白行吗?
【发布时间】:2017-09-17 22:44:56
【问题描述】:

我想用 trim 去掉空行:

line 1

line 2

得到

line1
line2

但使用

trim/lines

也会删除 CRLF。那么是否有另一种方法可以为此目的使用修剪?

【问题讨论】:

  • 您对 CRLF 的提及不清楚,因为您还使用了“空白行”表达式。 Rebol 不使用 CRLF 序列来标记新行,而是使用 LF 字符。那么您的意思是 LF,还是包含 CRLF 序列(ASCII 13 后跟 ASCII 10)的 Rebol 字符串?

标签: rebol


【解决方案1】:

你可以使用PARSE:

parse string-with-newlines [
    any [
          crlf remove some crlf
        | newline remove some newline
        | skip
    ]
]

不过使用字符集可能会更快:

text: complement charset crlf
parse string-with-newlines [
    any [
          some text
        | crlf any crlf
        | newline remove any newline
    ]
]

【讨论】:

    【解决方案2】:
    replace/all {Line1^/^/Line2} {^/^/} {^/}
    

    【讨论】:

      【解决方案3】:

      不可能只使用修剪,但这里有一个带有 removeach 的解决方案,还可以删除前导 LF

      trim-emptyline: func [
          str [string!] 
          /local lfb4 lfnow c
      ] [
          lfb4: true 
          remove-each c str [also all [lfnow: lf = c lfb4] lfb4: lfnow]
          str
      ]
      

      【讨论】:

        猜你喜欢
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-30
        • 1970-01-01
        • 2021-11-19
        • 2014-10-17
        • 1970-01-01
        相关资源
        最近更新 更多