2

Documentation > Swift > ClosedRange

let values = "a"..."z"
print(values.contains("c")) //true

Could someone please explain why this prints true?

  • That's essentially just doing `("a" <= "c") && ("c" <= "z")`. So your question essentially becomes "how does string comparison work?". – Oliver Charlesworth May 10 '17 at 12:52
  • I think the question is more for the first line. – Max May 10 '17 at 12:52
  • So there is an implementation of Comparable somewhere ? On String? –  May 10 '17 at 12:53
  • https://developer.apple.com/reference/swift/string/2427762 seems like the thing. – Oliver Charlesworth May 10 '17 at 12:55
  • Yes that is the comparable protocol. So is there an implementation of that on String for the English alphabet? –  May 10 '17 at 12:56
  • 1
    Compare [How String Comparison happens in Swift](http://stackoverflow.com/q/38910106/2976878). It's based on a lexicographic comparison of unicode scalar values when the string is put into Unicode Normalisation Form D. For a single ASCII letter, that's just a straightforward comparison of unicode scalars for those letters. – Hamish May 10 '17 at 12:58
  • This answered my question http://stackoverflow.com/a/25775112/1951992 –  May 10 '17 at 13:17

0 Answers0