user3322141
user3322141

Reputation: 158

How to return specific option type by splitting a string using scala?

I have a following string and I want to split it using scala

"myInfo": "name-name;model-model;number-10"

I want to split value of myInfo string such that I can access myName and its value seperately. e.g. myName:name, model:R210 etc

I am using following code to split string.

val data = (mainString \ "myInfo").as[String].split("\\;").map(_.split("\\-").toList)
  .collect { case key :: value :: _ => key -> value }.toMap

It gives me desired result.

I am using

data.get("name"),data.get("model"),data.get("number")

to access list. It gives me results in string type. While I want to get result of data.get("number") in integer format.

How do I get result of 'number' in integer format?

Upvotes: 0

Views: 495

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

data.get("number").map(_.toInt)

will return an Option[Int]

Upvotes: 2

Related Questions