Kotlin sublist from element

Viewed 124

I have a list and element:

val element = "b"
val myList = listOf("a", "b", "c", "d")

And I would like to have a sublist starting after a given element.

So the result would be:

"c", "d"

I wonder if there is a built-in function to achieve that.

2 Answers

You can use this:

 val newArray = myList.takeLastWhile { it != element }

There are generally several ways to achive this. Kasima's answer is very good, but you can also try it this way:

val sublist = myList.subList(myList.indexOf(element) + 1, myList.size)
Related