Indexing/Slicing 个 Scala 集合

Indexing/Slicing of Collections in Scala

我看到了一些对 Scala 集合进行操作的非常酷的方法,但我想知道如何在 Scala 中进行切片操作?我看到像 dropLeft take 这样的方法,但很想知道 Scala 中是否存在像索引或切片这样更简单的方法。

例如:

val aString = "I want this word" val aList = List(1,2,3,4)

应该return:

val slicedString = aString.slice(7,11) => "this" //JavaScript type

val slicedList = aList.slice(0,2) => List(1,2) //JavaScript type

或像 python 中那样进行索引:

val slicedString = aString(7:11) => "this"

val slicedList = aList(0:2) => List(1,2)

如果您费心查阅 ScalaDocs,您会找到您要找的东西。

aString.slice(7,11)  //res0: String = this
aList.slice(0,2)     //res1: List[Int] = List(1, 2)