从 scala.collection.immutable.Iterable[String] 中删除第一个和最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25029386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:27:21  来源:igfitidea点击:

remove first and last Element from scala.collection.immutable.Iterable[String]

scalacollectionsscala-collections

提问by Govind Singh

I am trying to convert my way of getting values from Form, but stuck some where

我正在尝试转换我从 获取值的方式Form,但在某些地方卡住了

val os= for {
  m <- request.body.asFormUrlEncoded
  v <- m._2
} yield v

osis scala.collection.immutable.Iterable[String]and when i print it in console

osscala.collection.immutable.Iterable[String]当我打印控制台

os map println

console

安慰

sedet impntc
sun
job
03AHJ_VutoHGVhGL70

i want to remove the first and last element from it.

我想从中删除第一个和最后一个元素。

回答by Chris Martin

Use dropto remove from the front and dropRightto remove from the end.

用于drop从正面dropRight移除和从末端移除。

def removeFirstAndLast[A](xs: Iterable[A]) = xs.drop(1).dropRight(1)

Example:

例子:

removeFirstAndLast(List("one", "two", "three", "four")) map println

Output:

输出:

two
three

回答by Kigyo

Another way is to use slice.

另一种方法是使用slice.

val os: Iterable[String] = Iterable("a","b","c","d")
val result = os.slice(1, os.size - 1) // Iterable("b","c")