从 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
remove first and last Element from scala.collection.immutable.Iterable[String]
提问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
os是scala.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")

