String Examples: StringLikeUse strings and the StringLike functions to process strings. Call capitalize and get lines.
This page was last reviewed on Oct 3, 2023.
Strings. A string has letters. It contains a certain value. With string manipulation methods, we can capitalize the first letter in this string.
With StringLike, we access many functions that act upon strings. To uppercase the first letter, we invoke "capitalize" on a string.
An example. Here we create a constant string. The val means it is constant and we cannot reassign "name." We then capitalize the string.
// This is a string. val name = "plutarch" // Use the capitalize function to uppercase the first letter. val cap = name.capitalize println(cap)
Lines. Here we use the lines property on a string. This returns an Iterator of strings—it separates the strings on their line breaks.
Detail We can use the for-loop to access all the strings returned by lines. The foreach function and a lambda expression also works.
val data = "cat\nbird\nfish and dog" // Loop over lines and print them. for (line <- data.lines) { println(line) } println() // Use foreach and a lambda expression to print lines. data.lines.foreach(println(_))
cat bird fish and dog cat bird fish and dog
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.
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.
// 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.
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.
Detail This 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.
def test(combined: String, left: String, right: String) = { // The equals operator tests the String's data. // ... It compares characters. if (combined == left + right) { println(combined, true) } else { println(combined, false) } } // 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.
Strip. With stripLineEnd we remove unwanted trailing newlines. And stripMargin provides more advanced whitespace-trimming behavior.
String theory. 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.
No updates found for this page.
© 2007-2023 Sam Allen.