`stringr::str_glue`:如何转义反斜杠(\)?

Posted

技术标签:

【中文标题】`stringr::str_glue`:如何转义反斜杠(\\)?【英文标题】:`stringr::str_glue`: how to escape backslash (\)?`stringr::str_glue`:如何转义反斜杠(\)?
【发布时间】:2021-07-22 00:37:52
【问题描述】:

我希望str_glue 在粘贴时保留\,但我不明白如何逃避它。

我有这个reprex 和我所做的逻辑尝试:(逃避逃逸等)

library(stringr)
var <- "darkness"

str_glue("Hello var my old friend \
I've come to talk with you again")
#> Hello darkness my old friend 
#> I've come to talk with you again

str_glue("Hello var my old friend \\
I've come to talk with you again")
#> Hello darkness my old friend I've come to talk with you again

str_glue("Hello var my old friend \\\
I've come to talk with you again")
#> Hello darkness my old friend I've come to talk with you again

由 reprex 包于 2021-04-28 创建 (v2.0.0)

我想要的输出:

"Hello darkness my old friend \
I've come to talk with you again"

【问题讨论】:


【参考方案1】:
var <- "darkness"
stringr::str_glue("Hello var my old friend \\\ \n I've come to talk with you again")
#> Hello darkness my old friend \ 
#> I've come to talk with you again

由reprex package (v2.0.0) 于 2021 年 4 月 28 日创建

【讨论】:

以上是关于`stringr::str_glue`:如何转义反斜杠(\)?的主要内容,如果未能解决你的问题,请参考以下文章

点液体如何转义反斜杠

如何使用单个反斜杠转义字符串的特殊字符

sed命令反斜杠的转义

如何转义 NSPredicate 格式字符串中的尾部反斜杠

python 如何快速转义路径字符串中的斜杠

如何防止 JSONKit 从 ASP.NET JSON 日期格式转义反斜杠?