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