Home
Scala
Slice Examples
This page was last reviewed on Dec 15, 2023.
Dot Net Perls
Slice, substring. A slice is part of a list—it may only contain the elements at a range of positions. A substring is a string slice. It might only contain some characters.
Shows a slice
We can take slices of strings in Scala. We take substrings with a start index, and a last index (not a length). List uses the same syntax.
Substring example. In Scala we can call the Substring method as in Java. But a string can also be sliced with the slice function. Some implicit conversions make this possible.
Argument 1 The first character index where we want to start a substring. To get the first character, we use 0.
Argument 2 The last index of our substring. This is not a character count or length—it should be larger than the first argument.
Shows a slice
object Program { def main(args: Array[String]): Unit = { val phrase = "a soft orange cat" // Use slice on a string from scala.collection.immutable.StringOps. // ... Use first index, second index. val result = phrase.slice(2, 2 + 4) println(s"[$result]") } }
[soft]
List example. A list is like a string, but has other types of elements like Ints or Doubles. We can use slice on a list. We use a first and a last index.
List
Here We begin a slice at the second element (index 1, value 3.5) and continue until the third index.
Next We begin at element 3 (index 2, value 10.3) and continue until the end of the list (with the list's length).
object Program { def main(args: Array[String]): Unit = { val points = List(1.5, 3.5, 10.3, 11.3) println(points) // Get slice of list. val slice1 = points.slice(1, 3) println(slice1) // Get slice until end of list. val slice2 = points.slice(2, points.length) println(slice2) } }
List(1.5, 3.5, 10.3, 11.3) List(3.5, 10.3) List(10.3, 11.3)
String slice, char. We can access single chars from a string with an index. But with slice, we can get one-char strings (strings of length 1). Strings are sometimes more useful than chars.
object Program { def main(args: Array[String]): Unit = { val letters = "scala" // This is a char. val result1 = letters(0) // This is a string. val result2 = letters.slice(0, 1) println(s"result1 = $result1, result2 = $result2") } }
result1 = s, result2 = s
Slice versus substring. The slice() and substring methods receive the same arguments. And they return the same results. The returned string parts are equal in this program.
object Program { def main(args: Array[String]): Unit = { val example = "California" // Slice and substring have the same results on a string. val firstPart = example.slice(1, 4) val firstPart2 = example.substring(1, 4) // Print results. println(s"$firstPart, $firstPart2") if (firstPart == firstPart2) { println(true) } } }
ali, ali true
Performance. I tested the slice method and the substring method on a string. In different versions of Scala, this benchmark may perform differently.
Version 1 This version of the code uses the slice() method on a string and tests its result.
Version 2 Here we use the substring() method on a string. This code does the same thing as version 1.
Result In Scala 3.3 in 2023, it is faster to use substring than to take slices from a string.
object Program { def main(args: Array[String]): Unit = { val data = "abcdef" // Warm up the JIT. val test = data.slice(3, 5) val test2 = data.substring(3, 5) val t1 = System.currentTimeMillis() // Version 1: use slice. for (i <- 0 to 100000000) { val part = data.slice(3, 5) if (part != "de") { println(false) } } val t2 = System.currentTimeMillis() // Version 2: use substring. for (i <- 0 to 100000000) { val part = data.substring(3, 5) if (part != "de") { println(false) } } val t3 = System.currentTimeMillis() // Print times. println(t2 - t1) println(t3 - t2) } }
911 ms, slice 550 ms, substring
With slice we remove elements from a collection. We specify the elements we want to keep—the rest vanish. An immutable, new collection is returned.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2023 (new example).
Home
Changes
© 2007-2024 Sam Allen.