src471
src471

Reputation: 91

String splitting with backslash

I have a string

mystring <- "\"3.825"

I just want the 3.825 out of that.

I have tried:

mystring <- strsplit(mystring, "\")# (error after completing with 3 double quotes)

mystring <- strsplit(mystring, "\\")# (error: invalid regular expression '\', reason 'Trailing backslash')

mystring <- strsplit(mystring, "\\\\")# (returns the string unchanged and unsplit)

Not sure what else to do

Upvotes: 0

Views: 485

Answers (2)

hwnd
hwnd

Reputation: 70722

It's an escaped quote, not a backslash and if that is your string, there is no need to use regex.

> mystring <- "\"3.825"
> substring(mystring, 2)
# [1] "3.825"

Upvotes: 2

akrun
akrun

Reputation: 886978

You can try

  sub('"', '', mystring)
  #[1] "3.825"

You can wrap it with as.numeric to convert it to numeric.

There is no backslash in the string. You can check it by printing. It is the escape character for "

cat(mystring, '\n')
#"3.825 

Try '"' on R console

 '"'
#[1] "\"" 

Upvotes: 4

Related Questions