Avatar ·

How to remove the last character from a string in Swift? (String, Character)

Hello everyone! I just started learning Swift and encountered what seems like a simple task: I need to remove the last character from a string. But the standard approaches I tried don't work. I have a string: ```swift var text = "Hello, world!" ``` I want to get "Hello, world" (without the exclamation mark). I tried several options, but the compiler gives errors or the result is not correct. I tried using `text.removeLast()`, but this is a method for arrays, and it apparently doesn't work for strings. I tried `text = text.dropLast()`, but Xcode complains about types. I also tried `text.substring(to: text.index(before: text.endIndex))`, but this looks too cumbersome, and I'm not sure it's the right way. Please tell me how to correctly and elegantly remove the last character from a string.