String Functions
drop(int) - drop drops the characters mentioned as arguments.
Can be used with capitalize function, which makes only the first letter capital.
Highly needed to be called directly in Big Data, speeds up string processing
take() - takes the number of characters mentioned in the argument
split() - splits the string with the given string into a string Array
getBytes - gives the ASCII values of chars
x.<tab> - will give available functions in the base class
String comparisons
== operator is used for comparing two strings and returns true or false
Comparing with null does not give null pointer exception, because first it checks size of both strings, If sizes are different, it returns false
On the other hand, if x.trim()==y.trim() gives Null pointer exception, because it first calls the function. null.trim() gives Null pointer exception
Println and variable assignments for strings
raw
println("Hello \n world") - prints hello, newline, world
In Scala, raw - means print as it is
println(raw"Hello \n world")
s-interpolator (Whenever a statement begins with s is understood by the compiler to replace $ with actual values)
To print values in java, we need + "" etc
In Scala, with s or call to Interpolator, its much easier, as below
Call to toUpperCase
"Hello World".map(c=>c.toUpper) is same as
"Hello World".toUpperCase (internally calls map method)
In big data processing, directly calling map avoids one extra method call and improves processing
TypeCasting from Str to int, long
drop(int) - drop drops the characters mentioned as arguments.
Can be used with capitalize function, which makes only the first letter capital.
Highly needed to be called directly in Big Data, speeds up string processing
scala> var x = "1: test is a string"
x: String = 1: test is a string
scala> x.drop(2).capitalize
res1: String = " test is a string"
take() - takes the number of characters mentioned in the argument
scala> var x = "row1:test is a string"
x: String = row1:test is a string
scala> x.take(4)
res1: String = row1
split() - splits the string with the given string into a string Array
scala> var x = "test is a string"
x: String = test is a string
scala> x.split(" ")
res2: Array[String] = Array(test, is, a, string)
scala> x.split("is")
res0: Array[String] = Array("test ", " a string")
getBytes - gives the ASCII values of chars
scala> x.getBytes
res1: Array[Byte] = Array(116, 101, 115, 116, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103)
x.<tab>
scala> var x ="test is a string"
x: String = test is a string
scala> var y = "hello"
y: String = hello
scala> x==y
res2: Boolean = false
Comparing with null does not give null pointer exception, because first it checks size of both strings, If sizes are different, it returns false
scala> y=null
y: String = null
scala> x==y
res3: Boolean = false
On the other hand, if x.trim()==y.trim() gives Null pointer exception, because it first calls the function. null.trim() gives Null pointer exception
scala> x.trim()==y.trim()
java.lang.NullPointerException Println and variable assignments for strings
raw
println("Hello \n world") - prints hello, newline, world
In Scala, raw - means print as it is
println(raw"Hello \n world")
scala> println("Hello \n world")
Hello
world
scala> println(raw"Hello \n world")
Hello \n world
scala> var str =raw"Hello \n world"
str: String = Hello \n world
scala> println(str)
Hello \n world
s-interpolator (Whenever a statement begins with s is understood by the compiler to replace $ with actual values)
To print values in java, we need + "" etc
In Scala, with s or call to Interpolator, its much easier, as below
scala> var name = "tanushree"
name: String = tanushree
scala> var comp="igate"
comp: String = igate
In Java
scala> println("name:"+name+"works in company:"+comp)
name:tanushreeworks in company:igate
In Scala
scala> println(s"name:$name works in company:$comp")
name:tanushree works in company:igate
Call to toUpperCase
"Hello World".map(c=>c.toUpper) is same as
"Hello World".toUpperCase (internally calls map method)
In big data processing, directly calling map avoids one extra method call and improves processing
scala> "Hello World".map(c=>c.toUpper)
res2: String = HELLO WORLD
scala> "Hello World".toUpperCase
res3: String = HELLO WORLD
TypeCasting from Str to int, long
scala> var numStr ="1234"
numStr: String = 1234
scala> numStr.toInt
res2: Int = 1234
scala> numStr.toLong
res3: Long = 1234