Scala – Get last two characters from string

Scala allows you to do this in a much cleaner way than the standard String API by leveraging the collections API (for which there is an implicit conversion from a java.lang.String into an IndexedSeq[Char]):

str takeRight 2

The fantastic thing about the API of course, is that it preserves the type representation of the original “collection” (i.e. String in this case)!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)