我有一个字符串
something,'' something,nothing_something,op nothing_something,'' cat,cat
我想实现我的输出
'' something,op nothing_something,cat
有什么方法可以实现?
答案 0 :(得分:1)
如果我正确理解了您的要求,则可以采用以下步骤的一种方法:
以下是示例代码,该示例代码适用于具有稍微更通用的逗号分隔值的字符串:
val str = "cats,a cat,cat,there is a cat,my cat,cats,cat"
val csvIdxList = (Stream from 1).zip(str.split(",")).toList
val csvMap = csvIdxList.toMap
val csvPairs = csvIdxList.combinations(2).toList
val csvContainedIdx = csvPairs.collect{
case List(x, y) if x._2.contains(y._2) => y._1
case List(x, y) if y._2.contains(x._2) => x._1
}.
distinct
// csvContainedIdx: List[Int] = List(3, 6, 7, 2)
val csvToKeepIdx = (1 to csvIdxList.size) diff csvContainedIdx
// csvToKeepIdx: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 5)
val strDeduped = csvToKeepIdx.map( csvMap.getOrElse(_, "") ).mkString(",")
// strDeduped: String = cats,there is a cat,my cat
将以上内容应用于示例字符串something,'' something,nothing_something,op nothing_something
将产生预期的结果:
strDeduped: String = '' something,op nothing_something
答案 1 :(得分:0)
首先在给定的Array
上使用split
命令创建一个用逗号分隔的单词String
,然后使用filter
和mkString
进行其他操作,如下所示:
s.split(",").filter(_.contains(' ')).mkString(",")
在Scala REPL中:
scala> val s = "something,'' something,nothing_something,op nothing_something"
s: String = something,'' something,nothing_something,op nothing_something
scala> s.split(",").filter(_.contains(' ')).mkString(",")
res27: String = '' something,op nothing_something
根据Leo C的评论,我与其他String
进行了如下测试:
scala> val s = "something,'' something anything anything anything anything,nothing_something,op op op nothing_something"
s: String = something,'' something anything anything anything anything,nothing_something,op op op nothing_something
scala> s.split(",").filter(_.contains(' ')).mkString(",")
res43: String = '' something anything anything anything anything,op op op nothing_something