在R中的另一个字符串中查找一个字符串 [英] Find a string in another string in R
问题描述
我想在R中的另一个字符串中找到一个字符串.字符串如下.我希望能够将字符串 a 与字符串 b 匹配,并且输出应该是 a == b
,它返回 TRUE
I want to find a string within another string in R. The strings are as follows. I want to be able to match string a to string b as and the out put should be a == b
which returns TRUE
a <- "6250;7250;6251"
b <- "7250"
a == b #FALSE
推荐答案
如果 b 等于 725
而不是 7250
,你还希望结果是 <代码>正确代码>?
If b were to equal 725
instead of 7250
, would you still want the result to be TRUE
?
如果是这样,那么已经给出的 grepl
答案将起作用(您可以通过设置 fixed=TRUE
来加快速度,因为没有要匹配的模式.
If so then the grepl
answer already given will work (and you could speed it up a bit by setting fixed=TRUE
since there are no patterns to be matched.
如果您只希望 TRUE
与 ;
之间的某些内容完全匹配,那么您将需要将 b
嵌入到常规中表达式(sprintf
可能有帮助),或者更简单,使用 strsplit
将 a
拆分为仅要匹配的部分,然后使用 %in%
查看 b
是否与这些值中的任何一个匹配.
If you only want TRUE
when there is an exact match to something between ;
then you will either need to embed b
into a regular expression (sprintf
may be of help), or simpler, use strsplit
to split a
into just the parts to be matched, then use %in%
to see if b
is a match to any of those values.
这篇关于在R中的另一个字符串中查找一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!