format and formatted StringsUse the formatted and format methods on format strings to insert values in placeholders.
Format. A string has values in it. For example, with "10 cats" we have the value 10, and some following text. With format, and formatted, we transform values and add surrounding text.
Standard format codes. With Java we have standard string format insertion points like "%d" for an Int. In Scala we keep the same format codes.
Formatted example. Let us begin with a simple example. We have a number 10 (specified with the val keyword). We call formatted() on this Int.
var, val
Tip The formatted() method can be called on any object—so we can use it on an Int, Double or String value.
Detail We pass a special format string to the formatted method. This uses the same format as String.format in Java.
val number = 10 // Use formatted on an Int to use the Int in a format string. val result = number.formatted("There are %d cats.") println(result)
There are 10 cats.
Format method. This is called on a format string. We pass it arguments that are placed in the format insertion codes. Here we insert an Int and a floating-point (Double) with "%d" and "%f."
// An example format string. val f = "I have %d dogs that weigh on average %f pounds." // An Int and Double for the format string. val count = 4 val averageWeight = 14.5 // Call format on the format string and print the result. val result = f.format(count, averageWeight) println(result)
I have 4 dogs that weigh on average 14.500000 pounds.
Padding. Here we apply right and left padding to Ints and strings to justify text. For padding, we use the "-10s" and "10s" format codes for right and left padding insertions.
Note The "%1" and "%2" codes refer to the first and second arguments passed to the format method.
Detail The format method accepts the same format codes as the Java String.format method.
val lefts = List(10, 200, 3000) val rights = List("cat", "fish", "elephant") // Loop over indexes. for (i <- 0.until(lefts.length)) { // Get element from each list at this index. val left = lefts(i) val right = rights(i) // Use this format string. val padding = "%1$-10s %2$10s" // Call format to apply padding. val result = padding.format(left, right) println(result) }
10 cat 200 fish 3000 elephant
Reorder, repeat values. We can use a format string and the format() method to reorder and repeat values in a string. Consider this example. We have 3 insertions.
And The second argument to format, "cat," is repeated twice. The first argument "frog" is inserted after the second.
// Specify the second argument is placed first. // ... We can reorder and repeat arguments. val result = "%2$s %1$s %2$s".format("frog", "cat") println(result)
cat frog cat
Java notes. A key advantage to Scala is that it can use the full power of the JVM. It is built on the JVM. We can use the format syntax from Java.
A summary. Formatting strings in Scala is done in a standard way (with the same literal syntax as Java). But with formatted() we can format values in a clearer way—with just a method call.
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.