Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, string formatting can be accomplished using the format
method and the f
interpolator. Additionally, the formatted
method can be used for simpler formatting tasks. Let's look at each of these methods.
format method:
The format
method is similar to Java's String.format
method. It uses format specifiers defined by java.util.Formatter
.
val formattedString = String.format("Hello %s, you have %d messages.", "Alice", 5) println(formattedString) // Outputs: Hello Alice, you have 5 messages.
f interpolator:
This is a more Scala-like way to format strings using string interpolation. By prefixing a string with f
, you can embed expressions inside ${...}
and provide format specifiers.
val name = "Bob" val age = 30 println(f"$name%s is $age%d years old.") // Outputs: Bob is 30 years old.
In the example above, %s
is used to format a String
and %d
is used to format an integer.
formatted method:
The formatted
method provides a way to format a single value. It's useful for quick formatting tasks.
val price = 12.456 println(price.formatted("%.2f")) // Outputs: 12.46
In the example, %.2f
formats the floating-point number to two decimal places.
Example combining them:
Let's combine the knowledge:
val name = "Charlie" val age = 25 val height = 1.85 val sentence = f"$name%s, aged $age%d, is $height%.2f meters tall." println(sentence) // Outputs: Charlie, aged 25, is 1.85 meters tall.
In summary, Scala provides multiple methods to format strings. While the f
interpolator offers a concise and idiomatic way to handle string formatting in Scala, the format
and formatted
methods can be useful in specific situations.
Using format
and formatted
in Scala:
The format
method is used to create formatted strings, and formatted
is an alternative syntax.
val name = "John" val age = 30 val formattedString = "%s is %d years old".format(name, age)
String Interpolation with formatted
in Scala:
String interpolation can be combined with formatted
for cleaner syntax.
val name = "Alice" val age = 25 val formattedString = s"$name is $age years old".formatted()
Formatting Numeric Values in Scala:
Use format specifiers to control the format of numeric values.
val pi = Math.PI val formattedPi = "Pi value: %.2f".format(pi)
Custom Formatting with formatted
Method in Scala:
Customize formatting using the formatted
method with placeholders.
val product = "Laptop" val price = 1200 val formattedString = "%s: $%d".formatted(product, price)
Advanced Formatting Options in Scala Strings:
Explore advanced formatting options such as alignment and width.
val alignExample = "%-10s|%10s".format("Left", "Right") val widthExample = "%20s".format("Wide text")
Formatting Dates and Times in Scala:
Format dates and times using java.text.SimpleDateFormat
or java.time.format.DateTimeFormatter
.
import java.text.SimpleDateFormat import java.util.Date val currentDate = new Date() val formattedDate = new SimpleDateFormat("dd-MM-yyyy").format(currentDate)
String Formatting and Pattern Matching in Scala:
Use pattern matching to extract and format values from strings.
val input = "John,30" val formatted = input match { case s"$name,$age" => s"$name is $age years old" case _ => "Invalid format" }
Comparing Formatted and printf
in Scala:
The printf
method provides a similar way to format strings.
val name = "Sam" val age = 28 printf("%s is %d years old\n", name, age)