In Kotlin, converting characters to strings and vice versa is a common task that can be accomplished in several ways. This article explores three different Kotlin Program to Convert Character to String and Vice-Versa providing detailed explanations, code implementations, and outputs for each example.
1. Converting Character to String
1.1 Using toString
Method
Program Explanation
The toString
method is a straightforward way to convert a character to a string in Kotlin. This method is available on any Kotlin Char
and converts it directly to a String
.
Code Implementation
fun main() {
val char = 'A'
// Convert character to string using toString() method
val string = char.toString()
println("Character: $char")
println("Converted String: $string")
}
Output
Character: A
Converted String: A
1.2 Using String Interpolation
Program Explanation
String interpolation in Kotlin can also be used to convert a character to a string. By embedding the character in a string template, Kotlin automatically converts the character to a string.
Code Implementation
fun main() {
val char = 'B'
// Convert character to string using string interpolation
val string = "$char"
println("Character: $char")
println("Converted String: $string")
}
Output
Character: B
Converted String: B
1.3 Using String Constructor
Program Explanation
The String constructor can be used to create a string from a character by passing the character as an argument to the constructor.
Code Implementation
fun main() {
val char = 'C'
// Convert character to string using String constructor
val string = String(charArrayOf(char))
println("Character: $char")
println("Converted String: $string")
}
Output
Character: C
Converted String: C
2. Converting String to Character
2.1 Using get
Method
Program Explanation
The get
method (or the []
operator) can be used to retrieve a character at a specific index from a string. When converting a single-character string to a character, you can access the first character using string[0]
.
Code Implementation
fun main() {
val string = "D"
// Convert string to character using get method
val char = string[0]
println("String: $string")
println("Converted Character: $char")
}
Output
String: D
Converted Character: D
2.2 Using single
Method
Program Explanation
The single
method ensures that the string contains exactly one character and returns that character. This method is useful for validating that the conversion is possible without errors.
Code Implementation
fun main() {
val string = "E"
// Convert string to character using single method
val char = string.single()
println("String: $string")
println("Converted Character: $char")
}
Output
String: E
Converted Character: E
2.3 Using first
Method
Program Explanation
The first
method returns the first character of a string. This method can be used when you are sure that the string is non-empty and want to convert its first character.
Code Implementation
fun main() {
val string = "F"
// Convert string to character using first method
val char = string.first()
println("String: $string")
println("Converted Character: $char")
}
Output
String: F
Converted Character: F
Conclusion
In this article, we explored various methods to convert characters to strings and vice versa in Kotlin. The first set of examples demonstrated how to convert characters to strings using the toString
method, string interpolation, and the String constructor. The second set of examples showed how to convert strings to characters using the get
method, the single
method, and the first
method.
Each method offers different advantages, and understanding these techniques allows you to handle character and string conversions efficiently in your Kotlin programs.