I've trying to get a substring of a string starting from index 1 in iOS 10 beta 6, and its a headache as swifts strings are constantly changing and much documentation is out of date and useless.
我试着从ios10 beta 6的索引1中获取一个字符串的子字符串,它让人头痛,因为swift字符串不断变化,很多文档都过时了,毫无用处。
String has the substring(from:Index), but it can't accept an integer (which has been the case for a while), so I was planning to use startIndex and advance it by 1, but now Index has no advanceBy method, so I cannot do this:
字符串有子字符串(from:Index),但是它不能接受整数(这在一段时间内是这样的),所以我打算使用startIndex并将其提前1,但是现在索引没有方法,所以我不能这样做:
let aString = "hello"
let subString = aString.substring(from: aString.startIndex.advanceBy(1))
How can I get a substring from index 1? How can you advance an index these days, and what is the point of the substring(from Index) method - how are you supposed to use it?
如何从索引1获得子字符串?如何在这些天内提高索引,以及子字符串(从索引)方法的要点是什么?您应该如何使用它?
2 个解决方案
#1
20
Looks pretty clear as the first item in the Swift 3 migration guide:
看起来很清楚,作为Swift 3迁移指南中的第一项:
The most visible change is that indexes no longer have
successor()
,predecessor()
,advancedBy(_:)
,advancedBy(_:limit:)
, ordistanceTo(_:)
methods. Instead, those operations are moved to the collection, which is now responsible for incrementing and decrementing its indices.最明显的变化是索引不再具有继承者()、前辈()、advancedBy(_:)、advancedBy(_:limit:)或distanceTo(_:)方法。相反,这些操作被转移到集合中,该集合现在负责对其索引进行递增和递减。
myIndex.successor() => myCollection.index(after: myIndex) myIndex.predecessor() => myCollection.index(before: myIndex) myIndex.advance(by: …) => myCollection.index(myIndex, offsetBy: …)
So it looks like you want something like:
所以看起来你想要的是:
let greeting = "hello"
let secondCharIndex = greeting.index(after: greeting.startIndex)
let enryTheEighthGreeting = greeting.substring(from: secondCharIndex) // -> "ello"
(Note also that if you want Collection
functionality—like index management—on a String
, it sometimes helps to use its characters
view. String
methods like startIndex
and index(after:)
are just conveniences that forward to the characters
view. That part isn't new, though... String
s stopped being Collection
s themselves in Swift 2 IIRC.)
(还要注意,如果想要在字符串上收集功能类似的索引管理,它有时会帮助使用它的字符视图。像startIndex和index(after:)这样的字符串方法只是对字符视图的方便。那部分并不新鲜,不过……在Swift 2 IIRC中,字符串不再是集合本身。
There's more about the collection indexes change in SE-0065 - A New Model for Collections and Indices.
在SE-0065中有更多关于集合索引的变化——一个新的集合和索引模型。
#2
6
Swift 3:
let str = "hello"
let subStr = str.substring(from:str.index(str.startIndex,offsetBy: 1))
print(subStr)//ello
Less verbose solution:
更少的详细解决方案:
print(str.substring(from:str.idx(index)))//ello
extension String{
func idx(_ index:Int) -> String.Index{
return self.index(self.startIndex, offsetBy: index)
}
}
#1
20
Looks pretty clear as the first item in the Swift 3 migration guide:
看起来很清楚,作为Swift 3迁移指南中的第一项:
The most visible change is that indexes no longer have
successor()
,predecessor()
,advancedBy(_:)
,advancedBy(_:limit:)
, ordistanceTo(_:)
methods. Instead, those operations are moved to the collection, which is now responsible for incrementing and decrementing its indices.最明显的变化是索引不再具有继承者()、前辈()、advancedBy(_:)、advancedBy(_:limit:)或distanceTo(_:)方法。相反,这些操作被转移到集合中,该集合现在负责对其索引进行递增和递减。
myIndex.successor() => myCollection.index(after: myIndex) myIndex.predecessor() => myCollection.index(before: myIndex) myIndex.advance(by: …) => myCollection.index(myIndex, offsetBy: …)
So it looks like you want something like:
所以看起来你想要的是:
let greeting = "hello"
let secondCharIndex = greeting.index(after: greeting.startIndex)
let enryTheEighthGreeting = greeting.substring(from: secondCharIndex) // -> "ello"
(Note also that if you want Collection
functionality—like index management—on a String
, it sometimes helps to use its characters
view. String
methods like startIndex
and index(after:)
are just conveniences that forward to the characters
view. That part isn't new, though... String
s stopped being Collection
s themselves in Swift 2 IIRC.)
(还要注意,如果想要在字符串上收集功能类似的索引管理,它有时会帮助使用它的字符视图。像startIndex和index(after:)这样的字符串方法只是对字符视图的方便。那部分并不新鲜,不过……在Swift 2 IIRC中,字符串不再是集合本身。
There's more about the collection indexes change in SE-0065 - A New Model for Collections and Indices.
在SE-0065中有更多关于集合索引的变化——一个新的集合和索引模型。
#2
6
Swift 3:
let str = "hello"
let subStr = str.substring(from:str.index(str.startIndex,offsetBy: 1))
print(subStr)//ello
Less verbose solution:
更少的详细解决方案:
print(str.substring(from:str.idx(index)))//ello
extension String{
func idx(_ index:Int) -> String.Index{
return self.index(self.startIndex, offsetBy: index)
}
}