String Examples
This page was last reviewed on Dec 15, 2023.
Dot Net Perls
Strings. 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.
Capitalize example. 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) } }
ToUpperCase, toLowerCase. String manipulation in Scala is done in a standard way. We call toUpperCase and toLowerCase to get copied and modified strings.
Tip With these functions, only letters are changed. A space is left alone. And already uppercased or lowercased letters are also ignored.
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
Multiply. 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.
Result The characters in the resulting 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 equals. In Scala the double-equals operator "==" compares the character data of Strings, not the object identities. Here we test string equality.
Info The test() method sees if the left and right parts are combined to equal the "combined" strings.
Result The 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.
String split
Strip. With stripLineEnd we remove unwanted trailing newlines. And stripMargin provides more advanced whitespace-trimming behavior.
String Strip
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2023 (edit).
© 2007-2024 Sam Allen.