Kotlin – Create Empty String
To create an empty String in Kotlin, assign the variable with empty double quotes ""
, or with String class constructor String()
.
The syntax to create an empty String using double quotes is
myStr = ""
The syntax to create an empty String using String constructor is
myStr = String()
Examples
ADVERTISEMENT
1. Create empty string using double quotes
In the following program, we will create an empty String using double quotes. To check if the string is empty or not, we will print String.isEmpty().
Main.kt
fun main(args: Array<String>) { var myStr = "" println("Is the String empty : ${myStr.isEmpty()}") }
Output
Is the String empty : true
2. Create empty string using String() constructor
In the following program, we will create an empty String using String constructor.
Main.kt
fun main(args: Array<String>) { var myStr = String() println("Is the String empty : ${myStr.isEmpty()}") }
Output
Is the String empty : true
Conclusion
In this Kotlin Tutorial, we learned how to create an empty String using double quotes or String constructor, with the help of examples.