Sunday 8 March 2009

Scala: Reversing a string by up- and then downcasing it

Did you know that you can reverse a string by merely upcasing it and then downcasing it again? Here's an example:

scala> val s = "ςσσ"
s: java.lang.String = ςσσ

scala> s.toUpperCase.toLowerCase == s.reverse.toString
res0: Boolean = true

scala>

If you don't believe me, just copy and paste the two lines of code above into the Scala interpreter, and see it for yourself.

2 comments:

Alex Cruise said...

...the operative word being a String, i.e. not just any String. :)

Nikolaj Lindberg said...

Darn --- you called my bluff, Alex :)

/nikolaj