Ad
Replace "%" With "\%" In String
I need to make a character vector suitable for use in LaTeX, so I need to escape all the percent signs with a single backslash. So far, all I've been able to do is to get two backslash:
> text <- c("This is 60%", "This is 30%")
> gsub("%", "\\%", text, fixed=TRUE)
[1] "This is 60\\%" "This is 30\\%"
> gsub("%", "\\\\%", text, fixed=TRUE)
[1] "This is 60\\\\%" "This is 30\\\\%"
As expected, if fixed=FALSE
I get no backslash using "\\%"
as a substitution or two backslash using `"\\%"' as a substitution. How can I get "This is 60\%" as a result?
Ad
Answer
In fact, the first version is already working:
text <- c("This is 60%", "This is 30%")
gsub("%", "\\%", text, fixed=TRUE)
[1] "This is 60\\%" "This is 30\\%"
When you see \\%
in the printed string, the first backslash is just an escape character for the second backslash, which is literal. Your output string is really this:
This is 60\%
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page
Ad