In Scala, how to get a slice of a list from nth element to the end of the list without knowing the length?

Just drop first n elements you don’t need:

List(1,2,3,4).drop(2)
res0: List[Int] = List(3, 4)

or in your case:

string.split("\n").drop(2)

There is also paired method .take(n) that do the opposite thing, you can think of it as .slice(0,n).

In case you need both parts, use .splitAt:

val (left, right) = List(1,2,3,4).splitAt(2)
left: List[Int] = List(1, 2)
right: List[Int] = List(3, 4)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)