Saturday, 15 August 2020

Scala: String interpolations

 In Scala, we can specify place holders in the string, where the place holder is replaced with an actual variable value. Place holder string must start with ‘s’ and the string can contain the variables delimited by $. 

Example

var name = "Krishna"

var age = 31

var msg = s"Hi $name, you are $age years old."

scala> var name = "Krishna"
var name: String = Krishna

scala> var age = 31
var age: Int = 31

scala> var msg = s"Hi $name, you are $age years old."
var msg: String = Hi Krishna, you are 31 years old.

scala> print(msg)
Hi Krishna, you are 31 years old.

You can embed some formulas in the interpolated string.

 

Example

${symbol*10} repeat the string ‘symbol’ 10 times.

scala> var symbol = "*"
var symbol: String = *

scala> var msg = s"I repeat 10 times. ${symbol*10}"
var msg: String = I repeat 10 times. **********




Previous                                                    Next                                                    Home

Friday, 14 August 2020

Scala: Triple quotes to handle multi line strings

You can use """ to define a string that spawns across multiple lines.

scala> var quote = """ Posted by Frederic Brussat on July 3, 2020
     | Share
     | Literary Wisdom
     | 
     | Hope is a renewable option: If you run out of it at the end of the day, you get to start over in the morning."""
var quote: String =
" Posted by Frederic Brussat on July 3, 2020
Share
Literary Wisdom

Hope is a renewable option: If you run out of it at the end of the day, you get to start over in the morning."



Previous                                                    Next                                                    Home

Scala: Concatenate strings

 Using ‘+’ operator, you can concatenate two or more strings.

Example

var str1 = "Hello"

var str2 = "World"

var str3 = str1 + " " + str2

scala> var str1 = "Hello"
var str1: String = Hello

scala> 

scala> var str2 = "World"
var str2: String = World

scala> 

scala> var str3 = str1 + " " + str2
var str3: String = Hello World

scala> print(str3)
Hello World



Previous                                                    Next                                                    Home

Scala: Define a string

 You can define a string using ‘String’ type.

Syntax

var variable_name : String = "message"

 

Example

var msg : String = "Hello, how are you

scala> var msg : String = "Hello, how are you"
var msg: String = Hello, how are you

scala> print(msg)
Hello, how are you

Since Scala infers type, you can omit String type while defining the variable.

scala> var name = "Krishna"
var name: String = Krishna

As you see, I defined the variable ‘name’ but not specified type. Scala infers the type automatically depends on the value it holds.



Previous                                                    Next                                                    Home

Scala: Working with Strings


Previous                                                    Next                                                    Home

Thursday, 13 August 2020

Scala: Data types

 Scala has exactly same types as Java, only difference is it do not have primitive type.

a.   Byte

b.   Short

c.    Int

d.   Long

e.   Float

f.     Double

g.   Char

h.   Boolean

Previous                                                    Next                                                    Home

Wednesday, 12 August 2020

Scala: semicolons are optional

 Semicolons are optional in Scala.

scala> {
     |   println("Hello World")
     |   println("How are you")
     | }
Hello World
How are you

If you want to keep semicolon, you can do so.

scala> {
     |   println("Hello World");
     |   println("How are you");
     | }
Hello World
How are you

Previous                                                    Next                                                    Home