2

Consider the string:

a <- "this is a string"

Now, grep can be used to confirm the presence of substrings:

grep("t",a)
grep("this",a)

But doesn't seem to give the location.

Is there a function that will give me the location of any substring?

getLoction(a, "t")
## 1 12

getLoction(a, "this")
## 1
Shambho
  • 3,250
  • 1
  • 24
  • 37

1 Answers1

2

Here you go, I'm partial to the stringr package:

library(stringr)
a <- "this is a string"
str_locate(a,"t")
str_locate(a,"this")
str_locate_all(a,"t")

and the output:

> str_locate(a,"t")
     start end
[1,]     1   1
> str_locate(a,"this")
     start end
[1,]     1   4
> str_locate_all(a,"t")
[[1]]
     start end
[1,]     1   1
[2,]    12  12
variable
  • 1,013
  • 1
  • 12
  • 29