首页 > 解决方案 > 如何通过从scala中的对象中删除Some来制作字符串?

问题描述

如何通过删除 Some 来制作逗号分隔的字符串

case class Animal(name:Option[String], size:Option[Int]=None)

val animal = Animal(Some("apple"),Some(67))

val result = animal.productIterator.mkString(",").toString

//this gives: "Some(apple),Some(67)"

//How to get : "apple,67"

标签: scalacollections

解决方案


试试这个:

val result = animal.productIterator.map {
    case Some(x) => x
    case x => x
  }.mkString(",")

推荐阅读