Swift 4
let str = "Mein String"
String bei Index
let index = str.index(str.startIndex, offsetBy: 3)
String(str[index]) // "n"
Teilstring
let startIndex = str.index(str.startIndex, offsetBy: 3)
let endIndex = str.index(str.startIndex, offsetBy: 7)
String(str[startIndex...endIndex]) // "Strin"
Erste n Zeichen
let startIndex = str.index(str.startIndex, offsetBy: 3)
String(str[..
`Letzte n Zeichen
let startIndex = str.index(str.startIndex, offsetBy: 3)
String(str[startIndex...]) // "String"
Swift 2 and 3
str = "Mein String"
String bei Index
Swift 2
let charAtIndex = String(str[str.startIndex.advancedBy(3)]) // charAtIndex = "n"
Swift 3
str[str.index(str.startIndex, offsetBy: 3)]
Teilstring von Index bis Index
Swift 2
let subStr = str[str.startIndex.advancedBy(3)...str.startIndex.advancedBy(7)] // subStr = "Strin"
Swift 3
str[str.index(str.startIndex, offsetBy: 3)...str.index(str.startIndex, offsetBy: 7)]
Erste n Zeichen
let first2Chars = String(str.characters.prefix(2)) // first2Chars = "Me"
Letzte n Zeichen
let last3Chars = String(str.characters.suffix(3)) // last3Chars = "ing"`