Scala Convert: String to Int, List and ArrayConvert string values to Ints. Change Lists and Maps to strings.
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 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).
Scala program that uses toString, toInt
val number = 123
// Convert Int to String.
val result = number.toString
if (result == "123"
// Convert String back to Int.
val original = result.toInt
if (original == 123
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.
Scala program that converts List to array
val colors = List("blue"
// Convert list to array.
val result = colors.toArray
// Print list.
// Print length and elements of the array.
List(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.
However We can specify a separator string as the second argument to addString. This is like a join method on list.
Scala program that converts list to string
val animals = List
// Create a StringBuilder to store all list element values.
builder1 = StringBuilder.newBuilder
// Call addString to add all strings with no separator.
val result1 = builder1.toString()
// Use a separator with addString.
// ... Converts a list to a string.
builder2 = StringBuilder.newBuilder
(builder2, "; "
val result2 = builder2.toString()
cat; bird; fish
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.
// Create a range from 10 to 20.
// ... Step is 3 after each element.
Scala program that converts range to list
range = 10.to
// Use toList to convert a range to a list.
val list = range.toList
Range(10, 13, 16, 19)
List(10, 13, 16, 19)
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.
// An empty vector.
Scala program that converts Vector to List
val vector = scala.collection.immutable.Vector
// Add 2 new values at the end.
val vector2 = vector :+
// Convert Vector to a List.
val result = vector2.toList
In Scala we find the StringLike type. This helps us convert and modify strings. As in Java, converting strings makes copies of existing strings.
Some notes. 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.
© 2007-2021 sam allen. send bug reports to email@example.com.