Character in kotlin :
In Kotlin, we use “Char” to represent a character. Also, we can create one character variable by using one single quote pair. For example, for the following program :
fun main(args: Array) {
val letter = 'c'
println("Current value of 'letter' is " + letter)
if(letter is Char){
println("It is a character")
}else{
println("It is not character")
}
}
It will print :
Current value of 'letter' is c
It is a character
So, even though we are not defining it as a character like var letter : Char = ‘c’, it is defined as “character”.
Escaped characters in Kotlin :
Escaped characters are special characters like new line , tab etc.These are escaped using one backslash. In kotlin, the supported escaped characters are : \t, \b, \n, \r, ’, ”, \ and $.
Few important functions of Char class :
- fun toByte(): Byte : It returns the value of the character as byte.
- fun toChar(): Char : Returns the value of the character as ‘Char’.
- fun toDouble(): Double : Returns the value of the character as ‘Double’.
- fun toFloat(): Float : Returns the value of the character as ‘Float’.
- fun toInt(): Int : Returns the value of the character as ‘Int’.
- fun toLong(): Long : Returns the value of the character as ‘Long’.
- fun toShort(): Short : Returns the value of the character as ‘Short
- equals : Returns ‘true’ if the value of one character is equal to another.
- fun Char.isDigit(): Boolean : Returns true if the character is a digit
- fun Char.isLetter(): Boolean : Returns true if the character is a letter
- isLowerCase : Returns ‘true’ if the given character is lowercase.
- isUpperCase : Returns ‘true’ if the given character is uppercase.
- toLowerCase : Converts the character to lowercase.
- toUpperCase : Converts the character to uppercase.