Avatar ·

How to properly work with substrings in Swift?

Hello everyone! I'm a beginner in Swift and I can't figure out how to extract a part of a string. In other languages like Python or JavaScript, this is done very simply: `"Hello"[0:4]` or `"Hello".substring(0, 4)`. But in Swift, everything seems complicated. Here's what I'm trying: ```swift let text = "Hello, world!" let sub = text[0...4] // Error: 'subscript' is unavailable ``` Xcode complains and says it's not allowed. Then I tried: ```swift let start = text.index(text.startIndex, offsetBy: 0) let end = text.index(text.startIndex, offsetBy: 4) let range = start...end let sub = text[range] // I get a Substring, not a String ``` I got some kind of `Substring`, not a regular string. How do I work with it? Why can't I just cut out a part of a string like in other languages? Why is everything so complicated?