【发布时间】:2023-01-04 18:36:30
【问题描述】:
如何用stringr::str_replace()、stringr::str_replace_all()添加转义字符?
我需要将字符串中的 ' 或 " 等字符替换为 \' (Can't -> Can\'t),但尝试 str_replace("Can't", "'", ("\'")) 无效。
【问题讨论】:
-
当然是重复的,但你可以做
stringr::str_replace("Can't", "'", ("\\\\'")) -
@sindri_baldur 也可以将其包装在
cat()中,以便他们可以看到实际输出