本文翻译自:How can I use String slicing subscripts in Swift 4?
I have the following simple code written in Swift 3: 我有以下用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: 从Xcode 9 beta 5,我收到以下警告:
'
substring(to:)
' is deprecated: Please useString
slicing subscript with a 'partial range from' operator. 不推荐使用'substring(to:)
':请使用带有'partial range from'运算符的String
切片下标。
How can this slicing subscript with partial range from be used in Swift 4? 如何在Swift 4中使用部分范围的切片下标 ?
#1楼
参考:https://stackoom.com/question/35aVE/如何在Swift-中使用String切片下标
#2楼
You should leave one side empty , hence the name "partial range". 您应将一侧留空 ,因此命名为“ partial range”。
let newStr = str[..<index]
The same stands for partial range from operators, just leave the other side empty: 运算符的部分范围相同,只是将另一侧留空:
let newStr = str[index...]
Keep in mind that these range operators return a Substring
. 请记住,这些范围运算符返回Substring
。 If you want to convert it to a string, use String
's initialization function: 如果要将其转换为字符串,请使用String
的初始化函数:
let newStr = String(str[..<index])
You can read more about the new substrings here . 您可以在此处阅读有关新子字符串的更多信息。
#3楼
Example of uppercasedFirstCharacter
convenience property in Swift3 and Swift4. Swift3和Swift4中的uppercasedFirstCharacter
便利属性的示例。
Property uppercasedFirstCharacterNew
demonstrates how to use String slicing subscript in Swift4. 属性uppercasedFirstCharacterNew
演示了如何在Swift4中使用字符串切片下标。
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"
#4楼
The conversion of your code to Swift 4 can also be done this way: 您的代码到Swift 4的转换也可以通过以下方式完成:
let str = "Hello, playground"
let index = str.index(of: ",")!
let substr = str.prefix(upTo: index)
You can use the code below to have a new string: 您可以使用下面的代码来创建新字符串:
let newString = String(str.prefix(upTo: index))
#5楼
substring(from: index) Converted to [index...] substring(from:index) 转换为 [index ...]
Check the sample 检查样品
let text = "1234567890"
let index = text.index(text.startIndex, offsetBy: 3)
text.substring(from: index) // "4567890" [Swift 3]
String(text[index...]) // "4567890" [Swift 4]
#6楼
Some useful extensions: 一些有用的扩展:
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)
}
}