Convert Char to String

Given s : String , how can I do the result

 s.first() 

in a String ?

+7
source share
3 answers

The string does not have a .first() function. Do you mean .head ?

Using head and returning a string is as simple as:

 s.head.toString 
+6
source

You can use the take method as follows:

 scala> val s = "abcdef" s: String = abcdef scala> val first = s.take(1) first: String = a scala> 
+12
source

Another variant:

 val s : String = "hello" val first : String = s(0)+"" 
0
source

All Articles