HomeSearch

Scala format and formatted Strings

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

Tip: The formatted() method can be called on any object—so we can use it on an Int, Double or String value.

Argument: We pass a special format string to the formatted method. This uses the same format as String.format in Java.

Scala program that uses formatted 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) Output 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."
Scala program that uses format // 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) Output 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.

Java: The format method accepts the same format codes as the Java String.format method.

Scala program that uses padding format 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) } Output 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.

Scala program that reorders, repeats values // 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) Output 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.String.format: 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls