在Ruby中查找字符串之间的区别

时间:2021-07-01 12:56:31

I need to take two strings, compare them, and print the difference between them.

我需要取两个字符串,比较它们,并打印它们之间的差异。

So say I have:

所以说我有:

teamOne = "Billy, Frankie, Stevie, John"
teamTwo = "Billy, Frankie, Stevie"

$ teamOne.eql? teamTwo 
=> false

I want to say "If the two strings are not equal, print whatever it is that is different between them. In this case, I'm just looking to print "John."

我想说“如果这两个字符串不相等,请打印它们之间的不同之处。在这种情况下,我只是想打印”John“。

4 个解决方案

#1


15  

All of the solutions so far ignore the fact that the second array can also have elements that the first array doesn't have. Chuck has pointed out a fix (see comments on other posts), but there is a more elegant solution if you work with sets:

到目前为止,所有解决方案都忽略了第二个阵列也可能具有第一个阵列不具备的元素的事实。 Chuck指出了一个修复(参见其他帖子的评论),但是如果你使用集合,有一个更优雅的解决方案:

require 'set'

teamOne = "Billy, Frankie, Stevie, John"
teamTwo = "Billy, Frankie, Stevie, Zach"

teamOneSet = teamOne.split(', ').to_set
teamTwoSet = teamTwo.split(', ').to_set

teamOneSet ^ teamTwoSet # => #<Set: {"John", "Zach"}>

This set can then be converted back to an array if need be.

如果需要,可以将该集转换回数组。

#2


4  

If the real string you are comparing are similar to the strings you provided, then this should work:

如果您要比较的真实字符串与您提供的字符串类似,那么这应该有效:

teamOneArr = teamOne.split(", ")
=> ["Billy", "Frankie", Stevie", "John"]
teamTwoArr = teamTwo.split(", ")
=> ["Billy", "Frankie", Stevie"]
teamOneArr - teamTwoArr
=> ["John"]

#3


4  

easy solution:

简单方案:

 def compare(a, b)
   diff = a.split(', ') - b.split(', ')
   if diff === [] // a and b are the same
     true
   else
     diff
   end
 end

of course this only works if your strings contain comma-separated values, but this can be adjusted to your situation.

当然,这仅适用于您的字符串包含逗号分隔值,但这可以根据您的情况进行调整。

#4


3  

You need to sort first to ensure you are not subtracting a bigger string from a smaller one:

您需要先排序以确保不从较小的字符串中减去较大的字符串:

def compare(*params)
   params.sort! {|x,y| y <=> x}
   diff = params[0].split(', ') - params[1].split(', ')
   if diff === []
      true
   else
      diff
   end 
end

puts compare(a, b)

#1


15  

All of the solutions so far ignore the fact that the second array can also have elements that the first array doesn't have. Chuck has pointed out a fix (see comments on other posts), but there is a more elegant solution if you work with sets:

到目前为止,所有解决方案都忽略了第二个阵列也可能具有第一个阵列不具备的元素的事实。 Chuck指出了一个修复(参见其他帖子的评论),但是如果你使用集合,有一个更优雅的解决方案:

require 'set'

teamOne = "Billy, Frankie, Stevie, John"
teamTwo = "Billy, Frankie, Stevie, Zach"

teamOneSet = teamOne.split(', ').to_set
teamTwoSet = teamTwo.split(', ').to_set

teamOneSet ^ teamTwoSet # => #<Set: {"John", "Zach"}>

This set can then be converted back to an array if need be.

如果需要,可以将该集转换回数组。

#2


4  

If the real string you are comparing are similar to the strings you provided, then this should work:

如果您要比较的真实字符串与您提供的字符串类似,那么这应该有效:

teamOneArr = teamOne.split(", ")
=> ["Billy", "Frankie", Stevie", "John"]
teamTwoArr = teamTwo.split(", ")
=> ["Billy", "Frankie", Stevie"]
teamOneArr - teamTwoArr
=> ["John"]

#3


4  

easy solution:

简单方案:

 def compare(a, b)
   diff = a.split(', ') - b.split(', ')
   if diff === [] // a and b are the same
     true
   else
     diff
   end
 end

of course this only works if your strings contain comma-separated values, but this can be adjusted to your situation.

当然,这仅适用于您的字符串包含逗号分隔值,但这可以根据您的情况进行调整。

#4


3  

You need to sort first to ensure you are not subtracting a bigger string from a smaller one:

您需要先排序以确保不从较小的字符串中减去较大的字符串:

def compare(*params)
   params.sort! {|x,y| y <=> x}
   diff = params[0].split(', ') - params[1].split(', ')
   if diff === []
      true
   else
      diff
   end 
end

puts compare(a, b)