Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
23
Task/Substring/Scala/substring.scala
Normal file
23
Task/Substring/Scala/substring.scala
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
object Substring {
|
||||
// Ruler 1 2 3 4 5 6
|
||||
// 012345678901234567890123456789012345678901234567890123456789012
|
||||
val str = "The good life is one inspired by love and guided by knowledge."
|
||||
val (n, m) = (21, 16) // An one-liner to set n = 21, m = 16
|
||||
|
||||
// Starting from n characters in and of m length
|
||||
assert("inspired by love" == str.slice(n, n + m))
|
||||
|
||||
// Starting from n characters in, up to the end of the string
|
||||
assert("inspired by love and guided by knowledge." == str.drop(n))
|
||||
|
||||
// Whole string minus last character
|
||||
assert("The good life is one inspired by love and guided by knowledge" == str.init)
|
||||
|
||||
// Starting from a known character within the string and of m length
|
||||
assert("life is one insp" == str.dropWhile(_ != 'l').take(m) )
|
||||
|
||||
// Starting from a known substring within the string and of m length
|
||||
assert("good life is one" == { val i = str.indexOf("good"); str.slice(i, i + m) })
|
||||
// Alternatively
|
||||
assert("good life is one" == str.drop(str.indexOf("good")).take(m))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue