Arrays 基于分割索引的Scala字符串过滤器

Arrays 基于分割索引的Scala字符串过滤器,arrays,string,scala,filter,Arrays,String,Scala,Filter,尝试编写一个函数,该函数将保留输入行的原始结构(不拆分)并基于某个索引进行筛选 下面的示例尝试过滤输入字符串,以便第4个元素(在管道上拆分后)大于2个标记 "1||2||3||4||test or not".split("\\|\\|").filter(_.map(line => line.split("\\s")(4).length>2)) I receive the following error; error: value split is not a member of

尝试编写一个函数,该函数将保留输入行的原始结构(不拆分)并基于某个索引进行筛选

下面的示例尝试过滤输入字符串,以便第4个元素(在管道上拆分后)大于2个标记

"1||2||3||4||test or not".split("\\|\\|").filter(_.map(line => line.split("\\s")(4).length>2))


I receive the following error;
error: value split is not a member of Char

如何修复此问题?

如果这样编写,您将能够看到省略的参数是一个字符串,然后是一个字符

    "this||2||is||my||test or not".split("\\|\\|").filter { someString =>
      someString.map { someChar =>
        someChar
      }
      true
    }
  }

filter
的长形式是
someCollection.filter(element=>booleanpression)
。类似于
map
。每次应用
.filter
.map
时,都会解构集合并对元素应用操作。
“…”.split(“…”)
的元素是字符串。字符串的“元素”是字符。

如果您这样写,您将能够看到省略的参数是字符串,然后是字符

    "this||2||is||my||test or not".split("\\|\\|").filter { someString =>
      someString.map { someChar =>
        someChar
      }
      true
    }
  }

filter
的长形式是
someCollection.filter(element=>booleanpression)
。类似于
map
。每次应用
.filter
.map
时,都会解构集合并对元素应用操作。
“…”.split(“…”)
的元素是字符串。字符串的“元素”是字符。

这应该满足您的需要。您可能希望为函数找到更好的名称

def predicate(index: Int, minSize: Int)(s: String): Boolean =
    s.split("\\|\\|") match {
        case e if e.length > index => e(index).split("\\s").length > minSize
        case _ => false
    }


lines.filter(predicate(4, 2))

这应该是你想要的。您可能希望为函数找到更好的名称

def predicate(index: Int, minSize: Int)(s: String): Boolean =
    s.split("\\|\\|") match {
        case e if e.length > index => e(index).split("\\s").length > minSize
        case _ => false
    }


lines.filter(predicate(4, 2))

这种情况的最终结果是什么?“此| | 2 | |是否为| |测试”?第4个元素将始终包含多个单词。我想过滤,这样新的数据结构只保留第4个元素大于2个单词的行。在这种情况下,最终结果是什么?“此| | 2 | |是否为| |测试”?第4个元素将始终包含多个单词。我希望进行筛选,以便新的数据结构只保留第4个元素大于2个单词的行