28.08.2016 Views

scala_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scala<br />

"My name is ".concat("Zara");<br />

Strings are more commonly concatenated with the + operator, as in:<br />

"Hello," + " world" + "!"<br />

Which results in:<br />

"Hello, world!"<br />

The following lines of code to find string length.<br />

object Demo {<br />

}<br />

def main(args: Array[String]) {<br />

}<br />

var str1 = "Dot saw I was ";<br />

var str2 = "Tod";<br />

println("Dot " + str1 + str2);<br />

Save the above program in Demo.<strong>scala</strong>. The following commands are used to compile and<br />

execute this program.<br />

\><strong>scala</strong>c Demo.<strong>scala</strong><br />

\><strong>scala</strong> Demo<br />

Output<br />

Dot Dot saw I was Tod<br />

Creating Format Strings<br />

You have printf() and format() methods to print output with formatted numbers. The String<br />

class has an equivalent class method, format(), that returns a String object rather than a<br />

PrintStream object.<br />

Try the following example program, which makes use of printf() method:<br />

object Demo {<br />

def main(args: Array[String]) {<br />

90

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!