Scala中nth模式匹配的索引



我有一个程序,我正在尝试查找字母" e"的第n个出现的索引。我猜到了这样的东西...

def findE(line: String, ignore: Int) : Int = {
    val pattern = "e".r
    val index = line.indexOf(pattern(ignore+1))
    index
}

其中

ignore+1 

是所需的组,但语法无效。想知道是否有人知道该怎么做?

如果我是你,我会使用标准组合。

> "abcdeabcdeabcde".zipWithIndex.collect {
    case ('e', index) => index
  }
res1: collection.immutable.IndexedSeq[Int] = Vector(4, 9, 14)

如果存在的话,请在索引5上拿任何东西,这是您的答案。

最新更新