Avatar ·

How to properly work with substring in Swift? Problems with String.Index and Range

Hi everyone! I recently started learning Swift and I'm having trouble getting a substring from a string. In other languages, like Python or JavaScript, this is very simple: `"Hello"[0:4]` or `"Hello".substring(0, 4)`. But in Swift I've encountered some strange things — `String.Index`, `Range`, and methods like `prefix()`, `suffix()`, `dropFirst()`. Here's an example that doesn't work for me: ```swift let text = "Привет, мир!" let substring = text[0...5] // Error: 'subscript(_:)' is unavailable ``` I tried to do it the way I'm used to, but the compiler complains. Then I learned that in Swift you can't just use integers for string indices because characters can have different lengths (for example, emoji). But how do I get the first 5 characters or a range from character 3 to 7?