How to access a character in a Scala String (or, an element in a sequence)
When coming to Scala from Java, the syntax for accessing a character at a position in a Scala String
is an interesting thing. You could use the Java charAt
method:
scala> "hello".charAt(0) res0: Char = h
However, the preferred (proper) approach to access a character in a String
is to use Scala’s Array
notation: