Scala has a built-in implicit conversion from java.lang.String to scala.runtime.RichString. Here are some things you can try:
- scala> val s = "hi"
- s: java.lang.String = hi
- scala> s.length
- res0: Int = 2
- scala> s(1)
- res1: Char = i
- scala> s(0)
- res2: Char = h
- scala> s.r
- res3: scala.util.matching.Regex = hi
- scala> s.drop(1)
- res4: scala.runtime.RichString = i
- scala> s.take(1)
- res5: scala.runtime.RichString = h
- scala> val longString = """
- | second line
- | third line
- | """
- longString: java.lang.String =
- second line
- third line
-
- scala> longString.lines
- res6: Iterator[String] = non-empty iterator
- scala> longString.lines.toList
- res7: List[String] = List(, second line, third line, )
- scala> longString.lines.map( _.length )
- res8: Iterator[Int] = non-empty iterator
No comments:
Post a Comment