字串擷取
- startIndex 前面數來
- endIndex 後面數來
index offsetBy
let helloIndex = cutString.index(cutString.startIndex, offsetBy: 5)
print(String(cutString.suffix(from: helloIndex)))
// -> " world"
print(String(cutString.prefix(upTo: helloIndex)))
// -> "hello"
// 從後面數來 第-5個
let worldIndex = cutString.index(cutString.endIndex, offsetBy: -5)
print(String(cutString.suffix(from: worldIndex)))
// -> "world"
print(String(cutString.prefix(upTo: worldIndex)))
// -> "hello "
index before 取之前
// 在index之前
print(String(cutString.suffix(from: cutString.index(before: cutString.endIndex))))
// -> ello world
print(String(cutString.prefix(upTo: cutString.index(before: cutString.endIndex))))
// -> h
index after 取之後
// 在index之後
print(String(cutString.suffix(from: cutString.index(after: cutString.startIndex))))
// -> ello world
print(String(cutString.prefix(upTo: cutString.index(after: cutString.startIndex))))
// -> h
直接給index類型
print(String(cutString.prefix(5)))
// -> "hello"
print(String(cutString[..<helloIndex]))
// -> "hello"
取範圍 第2~3個
print((cutBugString as NSString).substring(with: NSMakeRange(2,3)))
// -> "llo"
Swift更多文章
Swift字串拼接 文字拼接
Swift 字串擷取 文字擷取
Swift – 陣列轉字串 | Array to String | List to String | description
Swift Date 現在星期幾 這個月有幾天
Swift – 正規表達式 (電話/身分證/email)
Swift更多文章
Swift 彈出視窗 AlertController 的使用方法 💥
Swift UISegmentedControl 💻分段控制器!