I would like to find the location of a character in a string.
我想找到字符串中字符的位置。
Say: string = "the2quickbrownfoxeswere2tired"
说:字符串= " the2quickbrownfoxeswere2tired "
I would like the function to return 4
and 24
-- the character location of the 2
s in string
.
我希望函数返回4和24——字符串中2s的字符位置。
4 个解决方案
#1
84
You can use gregexpr
您可以使用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)
或者str_locate_all来自于stringr包这是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
note that you could simply use stringi
注意,您可以使用stringi
library(stringi)
stri_locate_all(pattern = '2', "the2quickbrownfoxeswere2tired", fixed = TRUE)
Another option in base R
would be something like
另一个以R为底的选项是类似的
lapply(strsplit(x, ''), function(x) which(x == '2'))
should work (given a character vector x
)
应该可以工作(给定字符向量x)
#2
26
Here's another straightforward alternative.
这是另一个简单的选择。
> which(strsplit(string, "")[[1]]=="2")
[1] 4 24
#3
12
You can make the output just 4 and 24 using unlist:
可以使用unlist使输出仅为4和24:
unlist(gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired"))
[1] 4 24
#4
1
find the position of the nth occurrence of str2 in str1(same order of parameters as Oracle SQL INSTR), returns 0 if not found
查找str1中第n次出现str2的位置(与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
#1
84
You can use gregexpr
您可以使用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)
或者str_locate_all来自于stringr包这是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
note that you could simply use stringi
注意,您可以使用stringi
library(stringi)
stri_locate_all(pattern = '2', "the2quickbrownfoxeswere2tired", fixed = TRUE)
Another option in base R
would be something like
另一个以R为底的选项是类似的
lapply(strsplit(x, ''), function(x) which(x == '2'))
should work (given a character vector x
)
应该可以工作(给定字符向量x)
#2
26
Here's another straightforward alternative.
这是另一个简单的选择。
> which(strsplit(string, "")[[1]]=="2")
[1] 4 24
#3
12
You can make the output just 4 and 24 using unlist:
可以使用unlist使输出仅为4和24:
unlist(gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired"))
[1] 4 24
#4
1
find the position of the nth occurrence of str2 in str1(same order of parameters as Oracle SQL INSTR), returns 0 if not found
查找str1中第n次出现str2的位置(与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