I want to get all the positions of "_" in my string:
MyString <- "Test_Test_Test"
grep("_", MyString)
This returns however:
[1] 1
What am I doing wrong?
Use gregexpr
rather than grep
MyString<-"Test_Test_Test"
> gregexpr('_', MyString)
[[1]]
[1] 5 10
attr(,"match.length")
[1] 1 1
attr(,"useBytes")
[1] TRUE
I'd recommend Hadley's stringr
package:
library("stringr")
MyString <- "Test_Test_Test"
str_locate_all(MyString, "_")
# [[1]]
# start end
# [1,] 5 5
# [2,] 10 10