1 - Create a extensions for String
with a new, wonderfull, superpower method called crop
:
extension String {
func crop(from: Int, length: Int) -> String! {
let startIndex = self.index(self.startIndex, offsetBy: from)
var result = self.substring(from: startIndex)
let endIndex = result.index(result.startIndex, offsetBy: length)
result = result.substring(to: endIndex)
return result
}
}
Now you can rest in peace:
var str = "12345678901"
var result = str.crop(from: 6, length: 3)
// results in "789"
Oh, many thanks. This is real helpful.
Found this gist using search phrase in google: "FUCKING SWIFT HOW TO SUBSTRING STRING"