【问题标题】:Keep quotation marks in a formatted list Racket在格式化列表中保留引号
【发布时间】:2019-09-18 02:17:44
【问题描述】:

如何打印带引号且没有反斜杠的格式化字符串?

例如当我进入时

(format "say ~a" "hello there!")

我想得到

" say "hello there!" "

我希望用引号将“hello there”括起来作为我输入的方式。但是,如果我将其格式化为字符串,结果如下:

"say \"hello there!\""

有没有办法在不使用反斜杠的情况下保留引号?

【问题讨论】:

    标签: list racket format-string


    【解决方案1】:

    评估字符串,然后print/println 将引用" 打印为\"

    也许你正在寻找display/displayln

    (displayln (format "say \"~a\"" "hello there!"))
    ; => say "hello there!"
    

    【讨论】:

    • (displayln (format "say ~v" "hello there!"))... 在格式字符串中使用\"~a\" 是一种反模式,应替换为~v
    • 正确。谢谢!
    【解决方案2】:

    使用~s 代替~a

    > (format "say ~s" "hello there!")`
    "say \"hello there!\""
    

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 2014-04-22
      • 2017-06-24
      • 1970-01-01
      • 2019-04-22
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多