scala - 如何通过从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"
解决方案
试试这个:
val result = animal.productIterator.map {
case Some(x) => x
case x => x
}.mkString(",")
推荐阅读
- opengl - OpenGL 4.4管道 - 裁剪前的透视分割?
- python - 如何在 Django 中使用我的 Product 模型中的字段填充我的 OrderItem 模型?
- vue.js - 在计算属性中连接多个 vue-loader 要求以定义 srcset 属性 Vue.js (Nuxt)
- django - 如何在 django 中的表单上获取用户名作为初始值
- excel - Excel VBA - 从工作表中删除表名
- wordpress - Find and Remove Grayscale Effect from Wordpress Gallery
- sql - 如何通过sas sql得到第n大的数
- d3.js - d3 rangeRound 的意外行为
- python - 在特定条件下获取数据集的特定值
- javascript - 下拉导航菜单隐藏所有页面内容