How to use substring in Swift 4? Method 'substring(to:)' is deprecated
Hello everyone! I just started learning Swift 4 and encountered a problem when working with strings. Previously, I saw in old tutorials that you can use the `substring(to:)` method to get a substring, but in Xcode 9 and Swift 4 this method is marked as deprecated.
Here's an example of the code I'm trying to write:
```swift
let fullString = "Hello, world!"
let index = fullString.index(of: ",")
let greeting = fullString.substring(to: index!)
```
Xcode gives a warning: `'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator`.
I googled it, but I don't quite understand how to properly use "string slicing subscript" with the `partial range from` operator. Can you explain with a simple example? I need to get the substring before the comma, i.e.
No answers yet.
Log in to leave an answer