Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
To get the Unicode value of a character at a specific index in a string, we can use the following code:
val str = "Hello, World!"
val index = 1
val unicode = str[index].code
println("The Unicode value of the character at index $index is $unicode")
In this example, we first define a string `str` and an index `index` to specify which character we want to get the Unicode value of. We then use the `code` property of the character at the specified index to get its Unicode value and store it in the `unicode` variable. Finally, we print out the Unicode value using string interpolation.
It’s important to note that the `code` property returns the Unicode value of a single character, not the entire string. If you want to get the Unicode values of all the characters in a string, you can use a loop to iterate over each character and get its Unicode value using the `code` property.