解释为什么这个字符串比较结果是错误的?

任何人都可以解释为什么下面的代码行结果是错误的。

如果字符串没有大写,代码返回true。 不应该忽略这个案子的意思,结果是一样的吗?

System.out.print( String("Carthorse".toCharArray().sortedArray()) .equals(String("Orchestra".toCharArray().sortedArray()),true) ) 

排序不会忽略个案,下面是您实际比较的内容:

 //Caehorrst vs. Oacehrrst 

尝试以下方法:

 val s1 = String("Carthorse".toLowerCase().toCharArray().sortedArray()) val s2 = String("Orchestra".toLowerCase().toCharArray().sortedArray()) println("$s1 vs. $s2, equal? ${s1==s2}") //acehorrst vs. acehorrst, equal? true 

或者多一点fun

 fun String.sortedCaseIgnore() = String(toLowerCase().toCharArray().sortedArray()) val s1 = "Carthorse".sortedCaseIgnore() val s2 = "Orchestra".sortedCaseIgnore() 

顺便说一下,使用println()来支持System.out.println() ,下面是它的定义(Kotlin标准库的一部分,不需要显式的导入):

 public inline fun println(message: Any?) { System.out.println(message) }