我有以下简单的代码写在Swift 3:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
在Xcode 9 beta 5中,我得到了以下警告:
'substring(to:)'已弃用:请使用带有'partial range from'操作符的字符串切片下标。
这个部分范围的切片下标如何在Swift 4中使用?
我有以下简单的代码写在Swift 3:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
在Xcode 9 beta 5中,我得到了以下警告:
'substring(to:)'已弃用:请使用带有'partial range from'操作符的字符串切片下标。
这个部分范围的切片下标如何在Swift 4中使用?
当前回答
快速4 5 5+
Last的子字符串
let str = "Hello World"
let removeFirstSix = String(str.dropFirst(6))
print(removeFirstSix) //World
来自First的子字符串
let removeLastSix = String(str.dropLast(6))
print(removeLastSix) //Hello
其他回答
用这个方法你可以得到字符串的特定范围。你需要传递起始索引和你想要的字符总数。
extension String{
func substring(fromIndex : Int,count : Int) -> String{
let startIndex = self.index(self.startIndex, offsetBy: fromIndex)
let endIndex = self.index(self.startIndex, offsetBy: fromIndex + count)
let range = startIndex..<endIndex
return String(self[range])
}
}
希望这将帮助更多:-
var string = "123456789"
如果你想在某个特定的索引后面加一个子字符串。
var indexStart = string.index(after: string.startIndex )// you can use any index in place of startIndex
var strIndexStart = String (string[indexStart...])//23456789
如果你想在末尾删除某个字符串后获得子字符串。
var indexEnd = string.index(before: string.endIndex)
var strIndexEnd = String (string[..<indexEnd])//12345678
还可以使用以下代码创建索引:—
var indexWithOffset = string.index(string.startIndex, offsetBy: 4)
如果你只是想获取一个特定字符的子字符串,你不需要先找到索引,你可以只使用prefix(while:)方法
let str = "Hello, playground"
let subString = str.prefix { $0 != "," } // "Hello" as a String.SubSequence
Swift 5,4
使用
let text = "Hello world"
text[0] // H
text[...3] // "Hell"
text[6..<text.count] // world
text[NSRange(location: 6, length: 3)] // wor
Code
import Foundation
public extension String {
subscript(value: Int) -> Character {
self[index(at: value)]
}
}
public extension String {
subscript(value: NSRange) -> Substring {
self[value.lowerBound..<value.upperBound]
}
}
public extension String {
subscript(value: CountableClosedRange<Int>) -> Substring {
self[index(at: value.lowerBound)...index(at: value.upperBound)]
}
subscript(value: CountableRange<Int>) -> Substring {
self[index(at: value.lowerBound)..<index(at: value.upperBound)]
}
subscript(value: PartialRangeUpTo<Int>) -> Substring {
self[..<index(at: value.upperBound)]
}
subscript(value: PartialRangeThrough<Int>) -> Substring {
self[...index(at: value.upperBound)]
}
subscript(value: PartialRangeFrom<Int>) -> Substring {
self[index(at: value.lowerBound)...]
}
}
private extension String {
func index(at offset: Int) -> String.Index {
index(startIndex, offsetBy: offset)
}
}
编程时,我经常用简单的A-Za-z和0-9组成的字符串。不需要困难的索引操作。这个扩展是基于普通的老左/中/右函数。
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)
}
}