Updated April 4, 2023
Introduction to Scala string format
String formatting is required in a programming language when we want to print our value of the variable between the string message. In this case, string format is required; this makes the code easy to read and understand. Also, by the use of string format, we can easily put our variable values r the dynamic values inside the string message by calling two of the methods available in Scala. Scala provides us with two methods to format our string which is in build. In the coming section, we will discuss more of these two methods in detail how to use them while programming, Scala provide us with two methods to format.
Syntax
As of now, we know that string format is used to format our output message, which can be either used in logs or kept in a database for future use. Let’s have a look at the syntax to format string in Scala in detail, see below;
1. format() method: In this method, we can pass different parameters according to our need. We can call it on any string message we are preparing and passing parameters inside it.
String.format(value1, value2, value3..)
2. formatted() method: This method can be used with any object in scala. It can be double, integer, or string. It can take up any object. See syntax below for better understating;
anyobject.formatted("your string here ....")
How to format string in Scala?
As of now, we know that we use the format in Scala to format our string. This format is handy for the programmer if they want to track the value separately with the message; also, this formatting helps keep logs, which is very helpful while identifying the errors in our application. In Scala, we have two methods to format our string messages; these are the in-build method of Scala; we do not need to include any library to use them, hence handy to use also. In the coming section, we will discuss the methods in detail, how they work, and their signature. Let’s see each of them in details see below;
1. format(): This method takes any number of parameters depend upon the variables we are passing while creating the string message. We can call this method on the string message. It has already defined some symbol to print the various object between the message. First, see its signature;
String.format(value1, value2, value3..)
Symbols defined by the format method ;
a) %d: This symbol is used to print an integer value inside the message in Scala. But this is most common and used in various programming languages like c, c++ etc. If you want to print any integer in between the string message, you can use %d as the symbol. For better understating, we will see one practice example how to use it see below;
Example:
“Hi, I have done %d sets o exercise today !!”
b) %f: This symbol is used to print any float value inside the message in Scala. But this is most common and used in various programming language’s like c, c++ etc. If you want to print any float in between the string message, you can use %f as the symbol. For better understating, we will see one practice example how to use it see below;
Example:
“Hi, Rate of interest for SBI bank for loan increases by %f this year !!!!”
c) %c: This symbol is used to print any character value inside the message in Scala. But this is most common and used in various programming language’s like c, c++ etc. If you want to print any character between the string message, you can use %c as the symbol there. For better understating, we will see one practice example how to use it see below;
Example:
“Hi, %c Bye!!!!”
d) %s: This symbol is used to print any string value inside the message in Scala. But this is most common and used in various programming languages like c, c++, etc. If you want to print any string in between the string message, you can use %c as the symbol. For better understating, we will see one practice example of how to use it see below;
Example:
“Hi, %s Bye!!!!”
2. formatted() method: By the use of this method, we can use any object to deal with. It can be an integer, double, float, or even string itself. We can pass our string message inside the method and format our sting as we want in scala. First, see its signature for a better understanding of its usage;
signature;
anyobject.formatted("message")
In the above lines of code, we call this method on an object, so basically, this method is designed to operate on the object only. Inside that, we can pass our string message containing the appreciative symbol to print the value of the object variable.
All the symbol will be the same for this method also. We can use them with any string message, even though they are very much common in any programming language.
Examples
Different examples are mentioned below:
Example #1
In this example, we are using the format() method in Scala to format our string.
Code:
object Main extends App{
// Your code here!
println("Using format() method in Scala ************************")
val m1 = "Hello i am inteteger %d"
val m2 = "Hello i am string %s"
val m3 = "Hello i am character %c"
val m4 = "Hello i am float %f"
val value1 = 15
val value2 = "Hello World !!"
val value3 = 'z'
val value4 = 10.1
val result1 = m1.format(value1)
val result2 = m2.format(value2)
val result3 = m3.format(value3)
val result4 = m4.format(value4)
println("result is ::::::")
println("result one is ::::::" + result1)
println("result two is ::::::" + result2)
println("result three is ::::::" + result3)
println("result four is ::::::" + result4)
}
Output:
Example #2
In this example, we are using the formatted() method in Scala to format our string.
Code:
object Main extends App{
// Your code here!
println("Using formatted() method in Scala ************************")
val m1 = "Hello i am inteteger %d"
val m2 = "Hello i am string %s"
val m3 = "Hello i am character %c"
val m4 = "Hello i am float %f"
val value1 = 15
val value2 = "Hello World !!"
val value3 = 'z'
val value4 = 10.1
val result1 = value1.formatted(m1)
val result2 = value2.formatted(m2)
val result3 = value3.formatted(m3)
val result4 = value4.formatted(m4)
println("result is ::::::")
println("result one is ::::::" + result1)
println("result two is ::::::" + result2)
println("result three is ::::::" + result3)
println("result four is ::::::" + result4)
}
Output:
Conclusion
By using these methods, we can format our string in any way, which can either be used for log purpose or use them to show proper and easy messages to the user. These are the in build methods, so it is very easy to use, readable, and easy to understand as well.
Recommended Articles
We hope that this EDUCBA information on “Scala string format” was beneficial to you. You can view EDUCBA’s recommended articles for more information.