Home > Blockchain >  convert double quotes to single quotes in string?
convert double quotes to single quotes in string?

Time:04-05

In R, if you have the following string, which is a vector and each element is in double quotes:

values <- c("hello","mr") 

How can one go about creating the following string instead?

"'hello','mr'"

As in how do i add in those single quotes, but ensure the entire string is one sentence wrapped in double quotes?

I can hard code this with paste0 but it's not the same thing.

CodePudding user response:

Simple stringr solution:

library(stringr)
str_c("'",values,"'", collapse = ",")

CodePudding user response:

With sQuote and paste:

paste(sQuote(values, q = FALSE), collapse = ",")
# [1] "'hello','mr'"
  • Related