如何在Swift 4中使用string切片下标?

我有以下简单的代码写在Swift 3中:

let str = "Hello, playground" let index = str.index(of: ",")! let newStr = str.substring(to: index) 

从Xcode 9 beta 5中,我得到以下警告:

' substring(to:) '已被弃用:请使用String切片下标与“部分范围”运算符。

如何在Swift 4中使用部分范围的切片下标

你应该留下一边空的 ,因此名称“部分范围”。

 let newStr = str[..<index] 

同样代表部分范围的操作员,只是把对方留空:

 let newStr = str[index...] 

请记住,这些范围运算符返回一个Substring 。 如果你想把它转换成一个string,可以使用String的初始化函数:

 let newStr = String(str[..<index]) 

你可以在这里阅读更多关于新的子string。

转换子string(Swift 3)到string切片(Swift 4)

例子在Swift 3,4:

 let newStr = str.substring(to: index) // Swift 3 let newStr = String(str[..<index]) // Swift 4 

 let newStr = str.substring(from: index) // Swift 3 let newStr = String(str[index...]) // Swift 4 

 let range = firstIndex..<secondIndex // If you have a range let newStr = = str.substring(with: range) // Swift 3 let newStr = String(str[range]) // Swift 4 

斯威夫特4

用法

 let text = "Hello world" text[...3] // "Hell" 

 import Foundation extension String { subscript(value: PartialRangeUpTo<Int>) -> Substring { get { return self[..<index(startIndex, offsetBy: value.upperBound)] } } subscript(value: PartialRangeThrough<Int>) -> Substring { get { return self[...index(startIndex, offsetBy: value.upperBound)] } } subscript(value: PartialRangeFrom<Int>) -> Substring { get { return self[index(startIndex, offsetBy: value.lowerBound)...] } } } 

你的代码到Swift 4的转换也可以这样完成:

 let str = "Hello, playground" let index = str.index(of: ",")! let substr = str.prefix(upTo: index) 

您可以使用下面的代码来创build一个新的string:

 _ = String (str.prefix(upTo: index)) 

Swift3和Swift4中uppercasedFirstCharacter便利属性的示例

属性uppercasedFirstCharacterNew演示了如何在Swift4中使用String切片下标。

 extension String { public var uppercasedFirstCharacterOld: String { if characters.count > 0 { let splitIndex = index(after: startIndex) let firstCharacter = substring(to: splitIndex).uppercased() let sentence = substring(from: splitIndex) return firstCharacter + sentence } else { return self } } public var uppercasedFirstCharacterNew: String { if characters.count > 0 { let splitIndex = index(after: startIndex) let firstCharacter = self[..<splitIndex].uppercased() let sentence = self[splitIndex...] return firstCharacter + sentence } else { return self } } } let lorem = "lorem".uppercasedFirstCharacterOld print(lorem) // Prints "Lorem" let ipsum = "ipsum".uppercasedFirstCharacterNew print(ipsum) // Prints "Ipsum" 

substring(from:index) 转换为 [index …]

检查样品

 let text = "1234567890" let index = text.characters.index(text.startIndex, offsetBy: 3) text.substring(from: index) // "4567890" [Swift 3] String(text[index...]) // "4567890" [Swift 4] 

一些有用的扩展:

 extension String { func substring(from: Int, to: Int) -> String { let start = index(startIndex, offsetBy: from) let end = index(start, offsetBy: to - from) return String(self[start ..< end]) } func substring(range: NSRange) -> String { return substring(from: range.lowerBound, to: range.upperBound) } } 

编程时,我经常只有简单的A-ZA-Z和0-9的string。 无需索引操作。 这个扩展是基于简单的旧左/中/右function。

 extension String { // LEFT // Returns the specified number of chars from the left of the string // let str = "Hello" // print(str.left(3)) // Hel func left(_ to: Int) -> String { return "\(self[..<self.index(startIndex, offsetBy: to)])" } // RIGHT // Returns the specified number of chars from the right of the string // let str = "Hello" // print(str.left(3)) // llo func right(_ from: Int) -> String { return "\(self[self.index(startIndex, offsetBy: self.length-from)...])" } // MID // Returns the specified number of chars from the startpoint of the string // let str = "Hello" // print(str.left(2,amount: 2)) // ll func mid(_ from: Int, amount: Int) -> String { let x = "\(self[self.index(startIndex, offsetBy: from)...])" return x.left(amount) } } 

这是我的解决scheme,没有任何警告,没有错误,但完美的let redStr: String = String(trimmStr[String.Index.init(encodedOffset: 0)..<String.Index.init(encodedOffset: 2)]) let greenStr: String = String(trimmStr[String.Index.init(encodedOffset: 3)..<String.Index.init(encodedOffset: 4)]) let blueStr: String = String(trimmStr[String.Index.init(encodedOffset: 5)..<String.Index.init(encodedOffset: 6)])