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.
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
exampleIn 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.
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
exampleA 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.
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
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
I tested the slice method and the substring method on a string
. In different versions of Scala, this benchmark may perform differently.
slice()
method on a string
and tests its result.substring()
method on a string
. This code does the same thing as version 1.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.