我使用diff方法来比较两个String的字节数组,但是给出了意外的(或者是预期的行为?)结果。
diff()
会为true
返回(subString diff originalString)
,但会为false
返回(originalString diff subString)
。如果任何人都可以解释这一点,那就太好了。代码示例&输出如下。
代码段
val subString : Array[Byte] = "cde".getBytes("UTF-8")
val originalString : Array[Byte] = "abcdefg".getBytes("UTF-8")
println(s"compare subString with originalString - ${((subString diff originalString).isEmpty).toString}")
println(s"compare originalString with subString - ${((originalString diff subString).isEmpty).toString}")
输出
compare subString with originalString - true
compare originalString with subString - false
答案 0 :(得分:5)
diff
不是比较;它就像设置差异一样,"第一个字符串中的字符是什么,而第二个字符串中没有?"
"abcd" diff "a" = "bcd"
"a" diff "abcd" = ""
所以这些几乎等同于:
"abc".toSet diff "cb".toSet = Set(a)
"abc" diff "cb" = "a"