String Operations

From mi-linux
Revision as of 02:16, 21 February 2011 by In0316 (talk | contribs)
Jump to navigationJump to search

Really, really, long strings

Instead of using " you can use """ (three double quote marks) as a string delimeter if you want to define a really, really long string that is split over several lines. Like this:

 
val really_long_string = """<html>
  <head>
    <title>Looooong Scala String</title>
  </head>
  <body>
    <h1>This is a very long string indeed!</h1>
  </body>
</html>"""
println(really_long_string)
  

Splitting strings

 
scala> val mystring = "Hello, I like Scala! It, does, rock."
mystring: java.lang.String = Hello, I like Scala! It, does, rock.


scala> mystring.split(",")
res0: Array[java.lang.String] = Array(Hello,  I like Scala! It,  does,  rock.)


scala> mystring.split(",").foreach((msg:String) => println(msg))
Hello
 I like Scala! It
 does
 rock.

scala>