How to get the nth character of a string in Swift?
Hi everyone! I just started learning Swift and encountered a problem that seems very simple, but I can't solve it. In other languages, like Python or C++, I can easily access a character of a string by index using square brackets: `str[5]`. But in Swift, this doesn't work!
Here's my example code:
```swift
let message = "Hello, world!"
let fifthCharacter = message[4] // Error: 'subscript(_:)' is unavailable
```
I get a compilation error. I understand that Swift is a strongly typed language and strings are more complex due to Unicode support and grapheme clusters. But I just need to get a character by index, for example, the fifth character in the string.
I tried using `message[4]`, `message[4] as Character`, but nothing helps. Online I read about some `S
No answers yet.
Log in to leave an answer