将Scala中重复出现的重复字符串替换为空

时间:2018-08-03 20:16:53

标签: scala

我有一个字符串

something,'' something,nothing_something,op nothing_something,'' cat,cat

我想实现我的输出

'' something,op nothing_something,cat

有什么方法可以实现?

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的要求,则可以采用以下步骤的一种方法:

  1. 用“,”分隔输入字符串,并创建索引CSV的列表并将其转换为Map
  2. 生成索引CSV的2种组合
  3. 检查每个索引CSV对并捕获包含在其他CSV中的任何CSV的索引
  4. 由于与捕获的索引相对应的CSV包含在其他一些CSV中,因此删除这些索引将导致我们希望保留的剩余索引
  5. 使用剩余的索引从CSV映射中查找CSV,并将其连接回字符串

以下是示例代码,该示例代码适用于具有稍微更通用的逗号分隔值的字符串:

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,然后使用filtermkString进行其他操作,如下所示:

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