Swift 3.0 遍历 String.Index 范围
Swift 3.0 iterate over String.Index range
Swift 2.2 可以实现以下功能:
let m = "alpha"
for i in m.startIndex..<m.endIndex {
print(m[i])
}
a
l
p
h
a
对于 3.0,我们得到以下错误:
Type 'Range' (aka 'Range') does not conform to protocol 'Sequence'
我正在尝试对 swift 中的字符串执行一个非常简单的操作——简单地遍历字符串的前半部分(或者更一般的问题:遍历字符串的范围)。
我可以做到以下几点:
let s = "string"
var midIndex = s.index(s.startIndex, offsetBy: s.characters.count/2)
let r = Range(s.startIndex..<midIndex)
print(s[r])
但这里我并没有真正遍历字符串。所以问题是:如何遍历给定字符串的范围。喜欢:
for i in Range(s.startIndex..<s.midIndex) {
print(s[i])
}
您可以使用 characters
属性 的 indices
属性 遍历字符串,如下所示:
let letters = "string"
let middle = letters.index(letters.startIndex, offsetBy: letters.characters.count / 2)
for index in letters.characters.indices {
// to traverse to half the length of string
if index == middle { break } // s, t, r
print(letters[index]) // s, t, r, i, n, g
}
来自 部分的 documentation 字符串和字符 - 计算字符 :
Extended grapheme clusters can be composed of one or more Unicode scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift do not each take up the same amount of memory within a string’s representation. As a result, the number of characters in a string cannot be calculated without iterating through the string to determine its extended grapheme cluster boundaries.
重点是我自己。
这行不通:
let secondChar = letters[1]
// error: subscript is unavailable, cannot subscript String with an Int
如果你想遍历 String
的字符,那么不用显式访问 String
的索引,你可以简单地使用 CharacterView
of the String
, which conforms to CollectionType
,允许你使用整洁的子测序方法,例如 prefix(_:)
等。
/* traverse the characters of your string instance,
up to middle character of the string, where "middle"
will be rounded down for strings of an odd amount of
characters (e.g. 5 characters -> travers through 2) */
let m = "alpha"
for ch in m.characters.prefix(m.characters.count/2) {
print(ch, ch.dynamicType)
} /* a Character
l Character */
/* round odd division up instead */
for ch in m.characters.prefix((m.characters.count+1)/2) {
print(ch, ch.dynamicType)
} /* a Character
l Character
p Character */
如果您想将循环中的字符视为字符串,只需使用上面的 String(ch)
。
关于您在下面的评论:如果您想访问 CharacterView
的范围,您可以轻松实现自己的 CollectionType
扩展(指定用于 Generator.Element
是 Character
) 同时使用 prefix(_:)
和 suffix(_:)
来产生给定的子集合,例如半开 (from..<to
) 范围
/* for values to >= count, prefixed CharacterView will be suffixed until its end */
extension CollectionType where Generator.Element == Character {
func inHalfOpenRange(from: Int, to: Int) -> Self {
guard case let to = min(to, underestimateCount()) where from <= to else {
return self.prefix(0) as! Self
}
return self.prefix(to).suffix(to-from) as! Self
}
}
/* example */
let m = "0123456789"
for ch in m.characters.inHalfOpenRange(4, to: 8) {
print(ch) /* \ */
} /* 4 a (sub-collection) CharacterView
5
6
7 */
使用以下内容:
for i in s.characters.indices[s.startIndex..<s.endIndex] {
print(s[i])
}
另一种选择是使用 enumerated()
例如:
let string = "Hello World"
for (index, char) in string.characters.enumerated() {
print(char)
}
或者 Swift 4 只需使用
let string = "Hello World"
for (index, char) in string.enumerated() {
print(char)
}
最好的方法是:-
let name = "nick" // The String which we want to print.
for i in 0..<name.count
{
// Operation name[i] is not allowed in Swift, an alternative is
let index = name.index[name.startIndex, offsetBy: i]
print(name[index])
}
有关详细信息,请访问 here
迭代字符串中的字符在 Swift 4:
中更清晰
let myString = "Hello World"
for char in myString {
print(char)
}
Swift 4:
let mi: String = "hello how are you?"
for i in mi {
print(i)
}
具体演示如何在Swift4中遍历字符串中的范围,我们可以在for
循环中使用where
过滤器,将其执行过滤到指定的范围:
func iterateStringByRange(_ sentence: String, from: Int, to: Int) {
let startIndex = sentence.index(sentence.startIndex, offsetBy: from)
let endIndex = sentence.index(sentence.startIndex, offsetBy: to)
for position in sentence.indices where (position >= startIndex && position < endIndex) {
let char = sentence[position]
print(char)
}
}
iterateStringByRange("string", from: 1, to: 3)
将打印 t
、r
和 i
Swift 4.2
简单:
let m = "alpha"
for i in m.indices {
print(m[i])
}
Swift 2.2 可以实现以下功能:
let m = "alpha"
for i in m.startIndex..<m.endIndex {
print(m[i])
}
a
l
p
h
a
对于 3.0,我们得到以下错误:
Type 'Range' (aka 'Range') does not conform to protocol 'Sequence'
我正在尝试对 swift 中的字符串执行一个非常简单的操作——简单地遍历字符串的前半部分(或者更一般的问题:遍历字符串的范围)。
我可以做到以下几点:
let s = "string"
var midIndex = s.index(s.startIndex, offsetBy: s.characters.count/2)
let r = Range(s.startIndex..<midIndex)
print(s[r])
但这里我并没有真正遍历字符串。所以问题是:如何遍历给定字符串的范围。喜欢:
for i in Range(s.startIndex..<s.midIndex) {
print(s[i])
}
您可以使用 characters
属性 的 indices
属性 遍历字符串,如下所示:
let letters = "string"
let middle = letters.index(letters.startIndex, offsetBy: letters.characters.count / 2)
for index in letters.characters.indices {
// to traverse to half the length of string
if index == middle { break } // s, t, r
print(letters[index]) // s, t, r, i, n, g
}
来自 部分的 documentation 字符串和字符 - 计算字符 :
Extended grapheme clusters can be composed of one or more Unicode scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift do not each take up the same amount of memory within a string’s representation. As a result, the number of characters in a string cannot be calculated without iterating through the string to determine its extended grapheme cluster boundaries.
重点是我自己。
这行不通:
let secondChar = letters[1]
// error: subscript is unavailable, cannot subscript String with an Int
如果你想遍历 String
的字符,那么不用显式访问 String
的索引,你可以简单地使用 CharacterView
of the String
, which conforms to CollectionType
,允许你使用整洁的子测序方法,例如 prefix(_:)
等。
/* traverse the characters of your string instance,
up to middle character of the string, where "middle"
will be rounded down for strings of an odd amount of
characters (e.g. 5 characters -> travers through 2) */
let m = "alpha"
for ch in m.characters.prefix(m.characters.count/2) {
print(ch, ch.dynamicType)
} /* a Character
l Character */
/* round odd division up instead */
for ch in m.characters.prefix((m.characters.count+1)/2) {
print(ch, ch.dynamicType)
} /* a Character
l Character
p Character */
如果您想将循环中的字符视为字符串,只需使用上面的 String(ch)
。
关于您在下面的评论:如果您想访问 CharacterView
的范围,您可以轻松实现自己的 CollectionType
扩展(指定用于 Generator.Element
是 Character
) 同时使用 prefix(_:)
和 suffix(_:)
来产生给定的子集合,例如半开 (from..<to
) 范围
/* for values to >= count, prefixed CharacterView will be suffixed until its end */
extension CollectionType where Generator.Element == Character {
func inHalfOpenRange(from: Int, to: Int) -> Self {
guard case let to = min(to, underestimateCount()) where from <= to else {
return self.prefix(0) as! Self
}
return self.prefix(to).suffix(to-from) as! Self
}
}
/* example */
let m = "0123456789"
for ch in m.characters.inHalfOpenRange(4, to: 8) {
print(ch) /* \ */
} /* 4 a (sub-collection) CharacterView
5
6
7 */
使用以下内容:
for i in s.characters.indices[s.startIndex..<s.endIndex] {
print(s[i])
}
另一种选择是使用 enumerated()
例如:
let string = "Hello World"
for (index, char) in string.characters.enumerated() {
print(char)
}
或者 Swift 4 只需使用
let string = "Hello World"
for (index, char) in string.enumerated() {
print(char)
}
最好的方法是:-
let name = "nick" // The String which we want to print.
for i in 0..<name.count
{
// Operation name[i] is not allowed in Swift, an alternative is
let index = name.index[name.startIndex, offsetBy: i]
print(name[index])
}
有关详细信息,请访问 here
迭代字符串中的字符在 Swift 4:
中更清晰let myString = "Hello World"
for char in myString {
print(char)
}
Swift 4:
let mi: String = "hello how are you?"
for i in mi {
print(i)
}
具体演示如何在Swift4中遍历字符串中的范围,我们可以在for
循环中使用where
过滤器,将其执行过滤到指定的范围:
func iterateStringByRange(_ sentence: String, from: Int, to: Int) {
let startIndex = sentence.index(sentence.startIndex, offsetBy: from)
let endIndex = sentence.index(sentence.startIndex, offsetBy: to)
for position in sentence.indices where (position >= startIndex && position < endIndex) {
let char = sentence[position]
print(char)
}
}
iterateStringByRange("string", from: 1, to: 3)
将打印 t
、r
和 i
Swift 4.2
简单:
let m = "alpha"
for i in m.indices {
print(m[i])
}