Search code examples
scala

Concatenate characters in Scala


Suppose we have a string "code". How would we concatenate any two characters? Say for example we need to concatenate last two characters,

str.init.last + str.last gives result as 201. How would we get de instead?


Solution

  • You can use string interpolation to make any combination of characters:

    scala> val code = "code"
    code: String = code
    
    scala> s"${code(1)}${code(3)}"
    res0: String = oe