查找字符串中字符的位置

我希望找到字符串中字符的位置。

说: string = "the2quickbrownfoxeswere2tired"

我希望函数返回 424—— string2的字符位置。

208886 次浏览

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)

Here's another straightforward alternative.

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

You can make the output just 4 and 24 using unlist:

unlist(gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired"))
[1]  4 24

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

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

You could use grep as well:

grep('2', strsplit(string, '')[[1]])
#4 24