Scala: convert string to Int or None

前端 未结 4 1292
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 06:47

I am trying to get a number out of an xml field

...
12
...

via

Some((recipe \\ \"Main\" \\         


        
4条回答
  •  旧时难觅i
    2020-12-05 07:19

    Scala 2.13 introduced String::toIntOption:

    "5".toIntOption                 // Option[Int] = Some(5)
    "abc".toIntOption               // Option[Int] = None
    "abc".toIntOption.getOrElse(-1) // Int = -1
    

提交回复
热议问题