String Format Using the “$” Sign
String literals is the name of the new way of formatting the strings. The code that is provided is a simple program that demonstrates the basic string manipulation and variable usage. In Kotlin, the main is the first function that gets executed when the program starts just like any other programming language.
Inside the main function, there are three lines of code. The first line initializes a variable named “v” and allocates it with the “Kotlin” value. In Kotlin, the “val” keyword is used to declare a read-only (immutable) variable. The value of “v” cannot be altered once it is allocated. The second line declares another variable named “s” and assigns it with a string value.
The value of “s” is determined by concatenating the “Hi,” string with the value of variable “v” using the string interpolation. In this case, “$v” is replaced with the value of “v” which results in the “Hi, Kotlin” string being assigned to “s”. The last line uses the “println” function to output the value of variable “s” to the console.
val v = "Kotlin"
val s= "Hi, $v"
println(s)
}
It’s important to note that this code snippet is quite simple and doesn’t involve any complex logic or control flow. The output is as follows:
Insert Multiple Values into the String
The string format concept can be used to insert multiple values into a single string. Therefore, we updated our code as displayed in the following illustration. The main() function variable which is “v1” is assigned the “Kotlin” value. The “v2” variable is assigned the “Steve” value. The integer variable age is assigned the integer value of 28. The variable “s” is initialized as a string variable. It uses the string interpolation to generate a new string.
In this case, it combines the values of “v1”, “v2”, and “age” into a single string. The string is defined as “Hi, $v1. I’m $v2 and I’m $age years old”. The “$” symbol is used to denote a variable or expression that should be evaluated and inserted into the string. So, in this case, “$v1” is replaced with the value of “v1”, “$v2” is replaced with the value of “v2”, and “$age” is replaced with the value of “age”. The resulting string that is assigned to “s” is “Hi, Kotlin. I’m Steve and I’m 28 years old”. The println() method is utilized to output the “s” string to the console.
val v1 = "Kotlin"
val v2 = "Steve"
val age = 28
val s = "Hi, $v1. I'm $v2 and I'm $age years old."
println(s)
}
The output of the previous code is the same as we expected which is shown in the following image:
String Format Using the “${x}” Sign
Now, to extend the functionality of any string literal, we make use of the “$” sign along with the curly brackets. In the attached code, the “v” variable is declared and assigned with the value of 1 using the “val” keyword which indicates that it is a read-only variable. Then, another variable which is “result” is declared and is assigned with a string value using the string interpolation. String interpolation is achieved using the ${} syntax within a string. Inside the ${}, the “v + 3” expression is evaluated which adds the value of “v”, i.e. 1, to 3, resulting in 4. The evaluated result is then interpolated into the string which creates the final value of the result as “v + 3 = 4”.
val v = 1
val result = "v + 3 = ${v + 3}"
println(result)
}
In the end, the “println” function is used to print the value of the result to the console which outputs “v + 3 = 4” when the code is executed.
Evaluate the Logical Expressions
Kotlin can utilize the “$” sign to evaluate any logical expression. To demonstrate that, we use the Kotlin code examples which are displayed in the following illustration. The purpose of the code is to determine whether a given number of “v” is even or odd, and then print the result. The variable “v” is declared and is assigned with a value of 2 within the main() program. The “output” variable is declared and is assigned with a string value that includes the value of “v” and finds out whether it is even or odd. The “if(v > 0) “Even” else “Odd”” expression is used as part of the string assignment to determine whether “v” is greater than zero. If it is, the “Even” string is used. Otherwise, the “Odd” string is used. The value of output is printed using the “println” function.
val v = 2
val output = "$v is ${if(v > 0) "Even" else "Odd" }"
println(output)
}
When you run this code, it outputs “2 is Even” since the value of “v” is 2 which is an even number. If “v” is assigned with an odd number, the output reflects that accordingly.
String Manipulation Using the “Format” Function
The use of the format() function to manipulate and create a new string is well-known among Kotlin users. Let’s look at its examples by declaring the “v1” variable that is assigned with the “Kotlin” value. The “v2” variable is assigned with the value of “Java” and “v3” is initially assigned with the value of v2 (which is “Java”).
The code then performs the string concatenation and assigns the resulting string back to “v3”. The v3 += ” is” expression appends the ” is” string to the value of “v3”, so “v3” becomes “Java is”. Then, v3 += ” a new string” appends the ” a new string” string to the value of “v3”, so “v3” becomes “Java is a new string”. Finally, the code uses string formatting to print the values of “v1” and “v3” in a specific format: “Format:\n%s\n%s”. The format(v1, v3) formats the string with two placeholders (%s) representing “v1” and “v3”. The \n represents a new line. The resulting formatted string is then printed using println().
val v1 = "Kotlin"
val v2 = "Java"
var v3 = v2
v3 += " is"
v3 += " a new string"
println("Format:\n%s\n%s".format(v1, v3))
}
In summary, the code combines the strings using concatenation (+=) and demonstrates the string formatting to display the values of “v1” and “v3” in a specific format.
Conclusion
Using the string formatting, you can easily construct the complex strings that incorporate the variable values in a readable and flexible manner. It promotes code readability and maintainability by separating the format logic from the values that are inserted into the string. We discussed how a string can be formatted using different ways in Kotlin.