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:

  1. Declare a String Variable:
  2. We declare a variable str and initialize it with the value "Hello, World!".

  3. Get Character at Index:

  4. We use the get method of the String class to retrieve the character at a specific index.
  5. In this example, we get the character at index 4 and assign it to the variable charAtIndex4.

  6. Print Character:

  7. We use the println function to print the character at index 4 along with a descriptive message.