Home
Scala
Convert: String to Int, List and Array
This page was last reviewed on Dec 15, 2023.
Dot Net Perls
Convert. A String contains a number like 20. But these are characters. To get an Int we must call a parsing method—we must convert the string.
Conversion and parsing. Scala 3.3 provides many built-in methods to convert Strings, Ints, Lists and Arrays. We use these for the clearest, smallest code.
ToString. Here we convert an Int to a string. And then we convert that string into an Int again. We use the toString def (part of scala.Any) and the toInt def (part of StringLike).
object Program { def main(args: Array[String]): Unit = { val number = 123 // Convert Int to String. val result = number.toString() if (result == "123") { println(result) } // Convert String back to Int. val original = result.toInt if (original == 123) { println(original) } } }
123 123
ToArray. In Scala we usually use Lists to store and manipulate data. But Arrays are also available—these are somewhat harder to work with.
Here We use toArray to convert the List to an Array. We print the values of the Array with a foreach call.
object Program { def main(args: Array[String]): Unit = { val colors = List("blue", "yellow", "red") // Convert list to array. val result = colors.toArray // Print list. println(colors) // Print length and elements of the array. println(result.length) result.foreach(println(_)) } }
List(blue, yellow, red) 3 blue yellow red
List to string, StringBuilder. With addString (a method on List) we can add all list elements to a StringBuilder. The result by default has no separators.
StringBuilder
However We can specify a separator string as the second argument to addString. This is like a join method on list.
object Program { def main(args: Array[String]): Unit = { val animals = List("cat", "bird", "fish") // Create a StringBuilder to store all list element values. val builder1 = new StringBuilder() // Call addString to add all strings with no separator. animals.addString(builder1) val result1 = builder1.toString() println(result1) // Use a separator with addString. // ... Converts a list to a string. val builder2 = new StringBuilder() animals.addString(builder2, "; ") val result2 = builder2.toString() println(result2) } }
catbirdfish cat; bird; fish
Range, list. In Scala we find a toList function that can convert many iterable collections into a list. This is part of scala.collection.TraversableOnce.
Here We convert a range (specified with the to and by methods) into a list, with the toList function.
Range
object Program { def main(args: Array[String]): Unit = { // Create a range from 10 to 20. // ... Step is 3 after each element. val range = 10.to(20).by(3) println(range) // Use toList to convert a range to a list. val list = range.toList println(list) } }
inexact Range 10 to 20 by 3 List(10, 13, 16, 19)
Vector, List. The Vector has different performance characteristics than a List: it can be added to and updated faster. We sometimes have a Vector, but need a List.
Tip With toList we can convert a Vector to a List. Here we have a Vector with 2 elements, and get a List with those same 2 elements.
Note Excess conversions of the Vector and List should be avoided. It is possible to directly use the Vector for storing elements.
object Program { def main(args: Array[String]): Unit = { // An empty vector. val vector = scala.collection.immutable.Vector.empty println(vector) // Add 2 new values at the end. val vector2 = vector :+ 1 :+ 5 println(vector2) // Convert Vector to a List. val result = vector2.toList println(result) } }
Vector() Vector(1, 5) List(1, 5)
StringLike. In Scala we find the StringLike type. This helps us convert and modify strings. As in Java, converting strings makes copies of existing strings.
String
Summary. Usually it is best to use the data type that is closest to the meaning of a value. So a string that means 100 is less effective than an Int of 100.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.