【问题标题】:Replace '\' by '/' in without using scan() or clipboard在不使用 scan() 或剪贴板的情况下将 '\' 替换为 '/'
【发布时间】:2019-07-10 12:25:20
【问题描述】:

无论在输入字符串中出现多少次,我都想将 '\' 替换为 '/'。

我已经阅读了这些:Replace "\" with "/" in r

Efficiently convert backslash to forward slash in R

但它们都提供了解决方案,通过剪贴板粘贴或通过 scan() 读取 directory_path。

我想将目录路径作为变量。

示例代码:

directory_path = 'D:\demo\app'

gsub( '\', '/', directory_path )

但它给出了错误。

> directory_path = 'D:\demo\app'
Error: '\d' is an unrecognized escape in character string starting "'D:\d"

【问题讨论】:

  • 像这样转义:test<-c("D:\\demo\\app").
  • 我想知道有多少关于 R 需要双重逃避 "\" 的问题。我怀疑他们有数百人。好吧,也许不是。这是搜索的结果,但大多数答案都不是重点:stackoverflow.com/search?q=%5Br%5D+replace+%27%5C%27
  • 路径将由用户输入,不能要求他用双斜杠输入。如何解决那部分?我为此提出了这个问题。尚未在其他问题中找到此表单的答案。

标签: r


【解决方案1】:

像这样逃避它们:

test <- c("D:\\demo\\app")

然后:

gsub("\\\\", "/", test)

【讨论】:

  • 目录路径将由不知道转义字符的用户输入。他会给出 'D:\demo\app' 并且不能被要求另外给出。如何解决?
猜你喜欢
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
相关资源
最近更新 更多