提取两个字符串之间不同的字符 [英] Extract characters that differ between two strings

查看:201
本文介绍了提取两个字符串之间不同的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 adist 来计算两个字符串之间不同的字符数:

  a<  - Happy day
b< - Tappy Pay
adist(a,b)#result 2
/ pre>

现在我想提取不同的字符。在我的例子中,我想得到字符串Hd(或TP



我试图查找 adist agrep

解决方案

您可以使用以下操作序列:




  • 使用 strsplit() li>
  • 使用 setdiff()来比较元素




尝试这样:

  strsplit(c(a,b),split =))
[1]Hd


I have used adist to calculate the number of characters that differ between two strings:

a <- "Happy day"
b <- "Tappy Pay"
adist(a,b) # result 2

Now I would like to extract those character that differ. In my example, I would like to get the string "Hd" (or "TP", it doesn't matter).

I tried to look in adist, agrep and stringi but found nothing.

解决方案

You can use the following sequence of operations:

  • split the string using strsplit().
  • Use setdiff() to compare the elements
  • Wrap in a reducing function

Try this:

Reduce(setdiff, strsplit(c(a, b), split = ""))
[1] "H" "d"

这篇关于提取两个字符串之间不同的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆