String
have characters, and may certain values. With string
manipulation methods in Scala 3.3, we can capitalize the first letter in this string
.
In this language, we can access many functions that act upon strings. To uppercase the first letter, we invoke "capitalize" on a string
.
Here we create a constant string
. The val
means it is constant and we cannot reassign "name." We then capitalize the string
.
object Program { def main(args: Array[String]): Unit = { // This is a string. val name = "plutarch" // Use the capitalize function to uppercase the first letter. val cap = name.capitalize println(cap) } }Plutarch
ToUpperCase
, toLowerCase
String
manipulation in Scala is done in a standard way. We call toUpperCase
and toLowerCase
to get copied and modified strings.
object Program { def main(args: Array[String]): Unit = { val name = "don quixote" // Uppercase all letters in the string. // ... The space is left unchanged. val upper = name.toUpperCase() println(upper) // Lowercase the letters. val lower = upper.toLowerCase() println(lower) } }DON QUIXOTE don quixote
Scala has special string
operators. The star operator (an asterisk) concatenates a string
the number of times we specify. This helps make whitespace and separators.
object Program { def main(args: Array[String]): Unit = { // Multiply this string (concatenate it repeatedly). val letters = "abc" * 3 println(letters) // Create a string of nine hyphens. val separator = "-" * 9 println(separator) } }abcabcabc ---------
Reverse
In some languages, we must develop custom string
reversal methods. But in Scala we can use reverse from scala collection IndexedSeqOptimized
.
string
are in reverse order. No custom function was needed to reverse a string
.object Program { def main(args: Array[String]): Unit = { val id = "x100" // Use reverse from scala.collection.IndexedSeqOptimized. val result = id.reverse // The characters in the string are now reversed. println(id) println(result) } }x100 001x
String
equalsIn Scala the double
-equals operator "==" compares the character data of Strings, not the object identities. Here we test string
equality.
test()
method sees if the left and right parts are combined to equal the "combined" strings.string
"abcd" is combined from "ab" and "cd." The strings characters are tested.object Program { def test(combined: String, left: String, right: String) = { // The equals operator tests the String's data. // ... It compares characters. if (combined == left + right) { println(s"$combined=true") } else { println(s"$combined=false") } } def main(args: Array[String]): Unit = { // These print true. test("abcd", "ab", "cd") test("catdog", "cat", "dog") // This prints false. test("xxyy", "ef", "gh") } }abcd=true catdog=true xxyy=false
Split
For strings with formatted data, split is often useful. We can invoke split in Scala. And with an array, we can split on more than one delimiter character.
With stripLineEnd
we remove unwanted trailing newlines. And stripMargin
provides more advanced whitespace-trimming behavior.
Scala provides helpful functions on strings. These enable us to manipulate and use strings without custom code. With capitalize, for example, we uppercase the first letter.