Other Languages on the JVM Village

Love Jython? Can't get enough JRuby? Groovy? Dynamic languages on the JVM can set you free! Scala? Sure! Let us know what you think here in the Other Languages on the JVM SIG

A super rich Java String with Scala

For the longest time I wish Java support HEREDOC kind of string construct like in Perl and other. This seems to be extremely useful when you writing SQL statements, help usage, and other text block. Fortunately, this is supported under Scala. Here is a simple example:

val sql = """select *from report
where entry_date between '2008-08-23' and '2008-07-23'
"""

Just like that and you have nice looking multi lines of valid java.lang.String object created.

There is even a convinient stripMargin method that works this way:
if(args(0) == "--help"){        
    println("""Usage: scala myprogram [options]
| This program is just a demo to show how
| to use multi-lines strings in Scala.
""".stripMargin)
}

Noticed that above I can use the tab or any space within my block of text, but I added a marker "|" character. The trailing method stripMargin will look for it and remove any space including the marker and thus produce the nice block that's intended.

There many more extra String methods that directly supported in Scala without you doing any extra. Go explore their API doc!

Have fun.
-Zemian