#############################
Python slices
Python
| Scala
|
>>> s = "hello python and scala"
>>> s
'hello python and scala'
>>> s[0]
'h'
>>> s[-1]
'a'
>>> s[:2]
'he'
>>> s[-2:]
'la'
>>> s[6:12]
'python'
>>> len(s)
22
>>> s[::-1]
'alacs dna nohtyp olleh'
>>> s[2:]
'llo python and scala'
>>> s[:-2]
'hello python and sca'
>>> s[1:]
'ello python and scala'
|
scala> val s = "hello python and scala"
s: String = hello python and scala
scala> s(0) // or: s.head
res0: Char = h
scala> s.last
res3: Char = a // as char
scala> s.takeRight(1)
res5: String = a // as string
scala> s.take(2)
res6: String = he
scala> s.takeRight(2)
res7: String = la
scala> s.substring(6, 12)
res11: String = python
scala> s.length
res12: Int = 22
scala> s.reverse
res14: String = alacs dna nohtyp olleh
scala> s.drop(2)
res18: String = llo python and scala
scala> s.dropRight(2)
res22: String = hello python and sca
scala> s.tail
res23: String = ello python and scala
|
More…
Python
| Scala
|
>>> "ke" in "joker"
True
>>> "joker".find("ke") // -1 if not found
2
|
scala> "joker" contains "ke"
res125: Boolean = true
scala> "joker".indexOf("ke") // -1 if not found
res126: Int = 2
|