1. Overview

While Java conversion methods are scattered across different classes and strategies, most of the conversions from String into the Scala data types are implemented in the StringOps class.

In this tutorial, we’ll explore how to convert some Scala data types to and from a String.

2. Dependencies

For this tutorial, we’ll be using the Scala REPL 2.13.0 for the code examples.

3. Numeric Conversions

Converting between String and numeric types such as Int, Long, Double, and Float are similar because they use the StringOps class, which offers equivalent methods for the four types. Let’s have a look.

3.1. Int Conversions

The first data type we’ll look at is Int.

Converting an Int to a String is handled using the toString method:

scala> val i: Int = 42
i: Int = 42

scala> i.toString
res0: String = 42

To convert between a String and an Int there are two options. First, we can use the toInt method:

scala> "42".toInt 
res0: Int = 42

The toInt method will throw an Exception in case of an incorrect number format:

scala> "42a".toInt 
java.lang.NumberFormatException: For input string: "42a"
 at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.base/java.lang.Integer.parseInt(Integer.java:652)
 at java.base/java.lang.Integer.parseInt(Integer.java:770)
 at scala.collection.StringOps$.toInt$extension(StringOps.scala:889)
 ... 28 elided

Next is the toIntOption method, which will return a Some[Int] if the value is valid, or it will return a None in the case of an incorrect number format:

scala> "42".toIntOption
res1: Option[Int] = Some(42)

scala> "42a".toIntOption
res2: Option[Int] = None

3.2. Long Conversions

As we said before, conversions between String and Long are very similar to what we did with Int in the previous section:

scala> val l: Long = 42
l: Long = 42

scala> l.toString
res0: String = 42

scala> "42".toLong
res1: Long = 42

scala> "42".toLongOption
res3: Option[Long] = Some(42)

If we were to attempt to run “42a”.toLong we would receive a NumberFormatException just like we had with Int.

3.3. Double Conversions

Once again, the same methods apply to Double:

scala> val d: Double = 42
d: Double = 42.0

scala> d.toString
res0: String = 42.0

scala> "42".toDouble
res1: Double = 42.0

scala> "42".toDoubleOption
res2: Option[Double] = Some(42.0)

Once again, running “42a”.toDouble would throw a NumberFormatException just like in the previous examples.

3.4. Float Conversions

Now the last numeric type, Float:

scala> val f: Float = 42
f: Float = 42.0

scala> f.toString
res0: String = 42.0

scala> "42".toFloat
res1: Float = 42.0

scala> "42".toFloatOption
res2: Option[Float] = Some(42.0)

4. Converting String to Boolean

Converting a String into a Boolean has the particularity that is case insensitive:

scala> val b: Boolean = true
b: Boolean = true

scala> b.toString
res0: String = true

scala> "true".toBoolean
res1: Boolean = true

scala> "true".toBooleanOption
res2: Option[Boolean] = Some(true)

scala> "true-x".toBoolean
java.lang.IllegalArgumentException: For input string: "true-x"
  at scala.collection.StringOps$.toBooleanImpl$extension(StringOps.scala:943)
  at scala.collection.StringOps$.toBoolean$extension(StringOps.scala)
  ... 28 elided

scala> "true-x".toBooleanOption
res3: Option[Boolean] = None

scala> "TrUe".toBoolean
res4: Boolean = true

5. Converting String to CharArray

Now let’s look at the CharArray data type. To convert a String into a CharArray, we rely once again on the StringOps methods:

scala> "string".toCharArray
res0: Array[Char] = Array(s, t, r, i, n, g)

To create a String from the CharArray, we’ll use the mkString method:

scala> Array('a','b','c').mkString
res0: String = abc

6. Converting String to ByteArray

The conversion between String and ByteArray is not as easy as the previous examples.

To create a ByteArray from a String, we’ll use the getBytes method from StringOps:

scala> "baeldung".getBytes 
res0: Array[Byte] = Array(98, 97, 101, 108, 100, 117, 110, 103)

However, to handle the reverse operation, we need an intermediary conversion between Byte and Char and then into a CharArray:

scala> (res0.map(_.toChar)).mkString
res1: String = baeldung

7. Conclusion

In this article, we looked at ways to convert between several Scala data types and String representations.


« 上一篇: Scala中的提升