Updated May 29, 2023
Introduction to Scala format
In a programming language, we can format our string in a meaningful format and provide a useful message to the user that contains the values. In scala, we have a different method to format our string; one of them is called ‘format’ as the name suggests it is used to format our string like we have in C language.
Syntax:
We can use the format to format string in scala; we can pass value between the string message that we want to display to a user. We can even pass parameters to the format() method.
variable.formate(param1, param2, param3 ..so on)
In the above syntax, we are passing different parameters inside the format method; this parameter will be the value for the string message.
Example:
Code:
val msg = "we have %d hands.!!"
val val1 = 2
val r = msg.format(val1)
This syntax will give you a better understanding of the syntax how to use this inside the program while formatting the string in scala.
How does format Function work in Scala?
As of now, we know that the format method is used to format the string in scala language. This method has different identifiers we can say to identify the value and place them inside the message of the string. It has %d, %f and so on like we have in C language as well. By the use of it, we can format our string in the way we want. Also, we can put our value inside the string only with or so many lines of code. This format method takes parameters as well inside it.
Scala provided us with two methods to format our string: a format and the other is formatted.
1. format()
This method can be used to format strings. This method uses the various identifier to format the string for us in scala.
- %d: If we specify this identifier, then it will represent an integer value. By the use of this identifier, we can place integer values into our string.
- %f: If we specify this identifier, then it will represent double or float values. By the use of this identifier, we can place either float or double values into our string and format it in scala.
- %c: If we specify this identifier, then it will represent the character value. By the use of this identifier, we can place character values into our string and format it in scala.
- %s: If we specify this identifier, then it will represent string values. By the use of this identifier, we can place string values into our message and format it in scala.
Syntax:
varable.format(param1, param2)
2. Formatted
This method can be used with any object, whether it is a string, float, double or integer. For example, we can pass a string inside it contain the identifier associated with it.
We will also see its syntax to understand better how to use this inside the program to format our object.
Syntax:
variable.formatted("we have %d hands.")
Now we have a basic understanding of the format method how it works; now, we will see one example of how to use this method in an actual program in scala to format our string and object.
Example:
Code:
object Main extends App{
// Your code here!
val message = "We have %d hands."
val x =2
val result = message.format(x)
println(result)
}
In the above example, we are creating one class named as Main here and extending App to run our program; this is the basic syntax for the scala class. After this, we are preparing one message here; inside this message string, we are passing an integer value as well. So for that, we are using the %d identifier. Immediately after this line, we are initializing the integer variable that we want to print between the string message. At the end, we are calling the format() method on the String object and passing the parameters inside this. In our case, we are passing the integer ‘x’ as an input here. So it will print the message at the end.
Examples of Scala format
Given below are the examples of Scala format:
Example #1
In this example, we are putting integer values inside the string and using the format method.
Code:
object Main extends App{
// Your code here!
//creating string object
val message = "Hello everyone i am integer %d "
//assigning value
val x = 100
//calling format method here
val result = message.format(x)
//print result
println("Result is ::: ")
println(result)
}
Output:
Example #2
In this example, we are putting float value inside the string and using the format method.
Code:
object Main extends App{
// Your code here!
//creating string object
val message = "Hello everyone i am float %f "
//assigning value
val x = 128.89
//calling format method here
val result = message.format(x)
//print result
println("Result is ::: ")
println(result)
}
Output:
Example #3
In this example, we are putting character values inside the string and using the format method.
Code:
object Main extends App{
// Your code here!
//creating string object
val message = "Hello everyone i am character %c "
//assigning value
val x = 'Z'
//calling format method here
val result = message.format(x)
//print result
println("Result is ::: ")
println(result)
}
Output:
Example #4
In this example, we are putting string values inside the string and using the format method.
Code:
object Main extends App{
// Your code here!
//creating string object
val message = "Hello everyone i am string %s "
//assigning value
val x = "Adding string here "
//calling format method here
val result = message.format(x)
//print result
println("Result is ::: ")
println(result)
}
Output:
Conclusion
By using this method, we can format our string to the desired format. Also, we can specify the values in the string message by using different identifiers available in scala format. These are the same identifier which is available in C language also to print the message. We can print any object like string, integer, float, double, character etc.
Recommended Articles
This is a guide to Scala format. Here we discuss the introduction; how does format function work in scala? and examples, respectively. You may also have a look at the following articles to learn more –