Skip to content

Instantly share code, notes, and snippets.

@Araglas88
Last active Oct 18, 2021
Embed
What would you like to do?
Scala-String-Functions.sc
val str = "This is a String"
println( "The character at position 4th is " + str.charAt(3))
output : The character at position 4th is s
val str = "This is a String"
println( "The UTF-16 value of 4th character is " + str.codePointAt(3))
The UTF-16 value of 4th character is 115
val str = "abcDe"
str.compareTo("abCde")
str.compareTo("Bbcd")
str.compareTo("abcde")
res0: Int = 32
res1: Int = 31
res2: Int = -32
val str = "abcDe"
str.compareToIgnoreCase("AbcDe")
res0: Int = 0
val str = "Dave"
str.concat("Smith")
res0: String = DaveSmith
"Phil" + "Dunphy"
res1: String = PhilDunphy
val str = "subway"
str.contains("bwa") //res0: Boolean = true
str.contains("bWa") //res1: Boolean = false
val str1: String = "subway"
val str2: String = "subway"
str1.contentEquals(str2) //true
val str3: StringBuffer = new StringBuffer("subway")
str1.contentEquals(str3) //true
val str1: String = "subway"
val str2: String = "way"
val str3: String = "wa"
str1.endsWith(str2) //true
str1.endsWith(str3) //false
val str1: String = "subway"
val str2: String = "subway"
str1.equals(str2) //true
val str1: String = "subway"
val str2: String = "SubWay"
str1.equalsIgnoreCase(str2) //true
val str1: String = "subway"
str1.getBytes()
res0: Array[Byte] = Array(115, 117, 98, 119, 97, 121)
Here we have listed down all the Scala String Functions with example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment