HomeSearch

Scala String Examples: StringLike

Use strings and the StringLike functions to process strings. Call capitalize and get lines.
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.
Scala program that uses capitalize on string // This is a string. val name = "plutarch" // Use the capitalize function to uppercase the first letter. val cap = name.capitalize println(cap) Output Plutarch
Lines. Here we use the lines property on a string. This returns an Iterator of strings—it separates the strings on their line breaks.

Loops: We can use the for-loop to access all the strings returned by lines. The foreach function and a lambda expression also works.

Def
Scala program that uses lines 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(_)) Output 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.

Scala program that uses toUpperCase, toLowerCase 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) Output 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.
Scala program that multiplies string // Multiply this string (concatenate it repeatedly). val letters = "abc" * 3 println(letters) // Create a string of nine hyphens. val separator = "-" * 9 println(separator) Output 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.

Scala program that uses reverse, IndexedSeqOptimized 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) Output 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.

Test: 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.

Scala program that uses equals operator 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") Output (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.Split
Strip. With stripLineEnd we remove unwanted trailing newlines. And stripMargin provides more advanced whitespace-trimming behavior.stripLineEnd
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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls