Kotlin get char in string
Kotlin code to get char in a string:
fun main() {
val str = "Hello, World!"
val charAtIndex4 = str.get(4)
println("Character at index 4: $charAtIndex4")
}
Explanation:
- Declare a String Variable:
We declare a variable
str
and initialize it with the value "Hello, World!".Get Character at Index:
- We use the
get
method of the String class to retrieve the character at a specific index. In this example, we get the character at index 4 and assign it to the variable
charAtIndex4
.Print Character:
- We use the
println
function to print the character at index 4 along with a descriptive message.