2017-06-01 98 views
3

我正在使用diff方法來比較兩個字符串的字節數組,但會給出意外的結果(或者它是預期的行爲?)結果。scala Array [Byte] diff

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 

回答

5

diff不是比較;它就像差別一樣,「第一個字符串中的字符是什麼,但第二個字符串中的字符不是什麼?」

"abcd" diff "a" = "bcd" 
"a" diff "abcd" = "" 

所以這些都是幾乎相當於

"abc".toSet diff "cb".toSet = Set(a) 
"abc" diff "cb" = "a" 
相關問題