I have the following simple code written in Swift 3:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
From Xcode 9 beta 5, I get the following warning:
'
substring(to:)
' is deprecated: Please useString
slicing subscript with a 'partial range from' operator.
How can this slicing subscript with partial range from be used in Swift 4?
Convert Substring (Swift 3) to String Slicing (Swift 4)
Examples In Swift 3, 4:
Hope this will help little more :-
If you want a substring after some particular index.
If you want a substring after removing some string at the end.
you can also create indexes with the following code :-
substring(from: index) Converted to [index...]
Check the sample
Some useful extensions:
Creating SubString (prefix and suffix) from String using Swift 4:
Output
If you want to generate a substring between 2 indices , use :