...

Cloud Computing - SCALA

Back to Course

Lesson Description


Lession - #687 SCALA STRING


SCALA String

This part takes you through the Scala Strings. In Scala, as in Java, a string is a immutable object, or at least, an object that can't be changed. Then again, objects that can be modified, similar to arrays, are called mutable objects. Strings are extremely helpful objects, in the remainder of this part, we present significant methods for java.lang.String class.

Creating a String
The following code can be utilized to make a String −

var welcoming = "Hi world!";


or on the other hand

var greeting:String = "Hi world!";
EXAMPLE

Model
object Demo {
   val welcoming: String = "Hi, world!"

   def main(args: Array[String]>
{ println( welcoming >
} }


Save the above program in Demo.scala. The following commands are utilized to compile and execute this program.
command

\>scalac Demo.scala
\>scala Demo
Yield
Hi, world!

String Length
Techniques used to acquire data about an object are known as accessor methods. One accessor technique that can be utilized with strings is the length(>
method, which returns the number of characters contained in the string object. Utilize the following code fragment to track down the length of a string −

Model
object Demo {
   def main(args: Array[String]>
{ var palindrome = "Speck saw I was Tod"; var len = palindrome.length(>
; println( "String Length is : " + len >
; } }
Save the above program in Demo.scala. The following commands are utilized to compile and execute this program. command

\>scalac Demo.scala
\>scala Demo
Yield

String Length is : 17
concatenating Strings
The String class incorporates a technique for connecting two strings −

string1.concat(string2>
;