Find the location of a character in string

198,238

Solution 1

You can use gregexpr

 gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired")


[[1]]
[1]  4 24
attr(,"match.length")
[1] 1 1
attr(,"useBytes")
[1] TRUE

or perhaps str_locate_all from package stringr which is a wrapper for gregexpr stringi::stri_locate_all (as of stringr version 1.0)

library(stringr)
str_locate_all(pattern ='2', "the2quickbrownfoxeswere2tired")

[[1]]
     start end
[1,]     4   4
[2,]    24  24

note that you could simply use stringi

library(stringi)
stri_locate_all(pattern = '2', "the2quickbrownfoxeswere2tired", fixed = TRUE)

Another option in base R would be something like

lapply(strsplit(x, ''), function(x) which(x == '2'))

should work (given a character vector x)

Solution 2

Here's another straightforward alternative.

> which(strsplit(string, "")[[1]]=="2")
[1]  4 24

Solution 3

find the position of the nth occurrence of str2 in str1(same order of parameters as Oracle SQL INSTR), returns 0 if not found

instr <- function(str1,str2,startpos=1,n=1){
    aa=unlist(strsplit(substring(str1,startpos),str2))
    if(length(aa) < n+1 ) return(0);
    return(sum(nchar(aa[1:n])) + startpos+(n-1)*nchar(str2) )
}


instr('xxabcdefabdddfabx','ab')
[1] 3
instr('xxabcdefabdddfabx','ab',1,3)
[1] 15
instr('xxabcdefabdddfabx','xx',2,1)
[1] 0

Solution 4

To only find the first locations, use lapply() with min():

my_string <- c("test1", "test1test1", "test1test1test1")

unlist(lapply(gregexpr(pattern = '1', my_string), min))
#> [1] 5 5 5

# or the readable tidyverse form
my_string %>%
  gregexpr(pattern = '1') %>%
  lapply(min) %>%
  unlist()
#> [1] 5 5 5

To only find the last locations, use lapply() with max():

unlist(lapply(gregexpr(pattern = '1', my_string), max))
#> [1]  5 10 15

# or the readable tidyverse form
my_string %>%
  gregexpr(pattern = '1') %>%
  lapply(max) %>%
  unlist()
#> [1]  5 10 15

Solution 5

You could use grep as well:

grep('2', strsplit(string, '')[[1]])
#4 24
Share:
198,238
ricardo
Author by

ricardo

I enjoy programming for work and as a pass-time; I mostly use R and Cpp (via Rcpp).

Updated on July 08, 2022

Comments

  • ricardo
    ricardo almost 2 years

    I would like to find the location of a character in a string.

    Say: string = "the2quickbrownfoxeswere2tired"

    I would like the function to return 4 and 24 -- the character location of the 2s in string.

  • 3pitt
    3pitt about 6 years
    how can we extract the integers from the lists/objects returned by your first 3 solutions?
  • Arani
    Arani over 5 years
    Use regexpr instead of gregexpr to get the integers easily. Or use unlist on the output as indicated in another answer below.
  • francoiskroll
    francoiskroll about 5 years
    Can you explain what the [[1]] does?
  • Prafulla
    Prafulla about 5 years
    @francoiskroll, [[1]] represents the first element of the list.