Scala – Strings

  • date 8th October, 2020 |
  • by Prwatech |

A string is an arrangement of characters. In Scala, objects of String are permanent which implies a consistent and can’t be changed once made.

Use case1:

Creating a Scala String

scala> var word=”Prwatech”

var word: String = Prwatech

scala> val word=”Prwatech”

val word: String = Prwatech

Use case 2:

Find Length of a Scala String

scala> “Prwatech”.length()

val res3: Int = 8

scala> “”.length()

val res4: Int = 0

scala> ” “.length()

val res5: Int = 1

Use case 3:

Concatenating Scala Strings

scala> “prwa”.concat(“tech”)

val res6: String = prwatech

scala> “Mark”.concat(” “.concat(“Zuckerberg”))

val res7: String = Mark Zuckerberg

scala> “Mark”+” “+”Zuckerberg”

val res8: String = Mark Zuckerberg

Use case 4:

Creating Format Strings in Scala

scala> var (a:Int,b:Int,c:Int)=(11,12,13)

var a: Int = 11

var b: Int = 12

var c: Int = 13

scala> printf(“a=%d, b=%d, c=%d”,a,b,c)

a=11, b=12, c=13

Quick Support

image image