I have a string 'ABC1\001ABCEFCGJS' I want to extract only 001ABCEFCGJS from this string How to do so in R? My String will be a dynamic string. So the solution should be such that function can read anything after backslash.
Asked
Active
Viewed 94 times
-2
-
2Can you share your string using `dput` ? Do you need `sub(".*\\\\", '','ABC1\\001ABCEFCGJS')` ? – Ronak Shah Dec 11 '19 at 02:13
-
My String is str<-'ABC1\001ABCEFCGJS' – Arpit Gupta Dec 11 '19 at 02:44
-
If that is your string then it does not have any backslash. Print `cat(str)` to see the actual string. – Ronak Shah Dec 11 '19 at 02:46
-
yes but can you please suggest how to extract '001ABCEFCGJS' this part of the string from my original string ('ABC1\001ABCEFCGJS')? – Arpit Gupta Dec 11 '19 at 02:49
1 Answers
2
One option would be to convert to raw
and then remove the elements, while appending the correct raw value for '001'
rawToChar(c(charToRaw('001'),charToRaw(str1)[-(1:5)]))
#[1] "001ABCEFCGJS"
data
str1 <- 'ABC1\001ABCEFCGJS'

akrun
- 874,273
- 37
- 540
- 662
-
Thanks for this reply,but my string will be a dynamic one. So my purpose is to extract anything in a string after the backslash. – Arpit Gupta Dec 11 '19 at 05:37