问题
I want to add the "\" character to my string using R. My string looks like this:
q <- "U0E2BU0E25"
I want to add a backslash before the letter "U" so the result would look like this:
\U0E2B\U0E25
I have tried using gsub:
gsub("U", "\U", q)
but received an error:
Error: '\U' used without hex digits in character string starting ""\U"
回答1:
We need to escape the backslash.
gsub("U", "\\\\U", q)
#[1] "\\U0E2B\\U0E25"
Note that there is an escape for the backslash. It becomes evident when we print it
cat(gsub("U", "\\\\U", q), "\n")
#\U0E2B\U0E25
来源:https://stackoverflow.com/questions/44366812/add-backslash-before-a-character-in-a-string