我想查找字符串中字符的位置。例如:
string =“the2quickbrownfoxeswere2tired”
。我希望函数返回 2
的位置,即字符串
中2
的字符位置为4
和24
。gregexpr
。 gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired")
[[1]]
[1] 4 24
attr(,"match.length")
[1] 1 1
attr(,"useBytes")
[1] TRUE
或者使用包含在stringr
包中的str_locate_all
函数,它是gregexpr
stringi::stri_locate_all
的一个封装(从stringr
版本1.0开始)
library(stringr)
str_locate_all(pattern ='2', "the2quickbrownfoxeswere2tired")
[[1]]
start end
[1,] 4 4
[2,] 24 24
请注意,您可以简单地使用 stringi
。
library(stringi)
stri_locate_all(pattern = '2', "the2quickbrownfoxeswere2tired", fixed = TRUE)
在基本的R
中,另一个选项可能是类似于以下内容:
lapply(strsplit(x, ''), function(x) which(x == '2'))
应该可以工作(假设给定一个字符向量x
)
regexpr
而不是gregexpr
可以轻松获取整数。或者按照下面另一个答案中所示,在输出上使用unlist
。 - Arani这是另一种直接的选择。
> which(strsplit(string, "")[[1]]=="2")
[1] 4 24
[[1]]
是什么意思吗? - francoiskroll您可以使用unlist将输出设置为4和24:
unlist(gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired"))
[1] 4 24
查找str2在str1中第n个出现的位置(参数顺序与Oracle SQL INSTR相同),如果没有找到则返回0
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
grep
命令:grep('2', strsplit(string, '')[[1]])
#4 24
要仅找到第一个位置,请使用lapply()
和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
要仅查找最后的位置,请使用lapply()
和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
.indexOf()
或其他类似的函数吗? - fge