用SWIFT编程语言获取字符串的第n个字符

用SWIFT编程语言获取字符串的第n个字符

我怎样才能得到字符串的第n个字符?我试过支架([])没有运气的访问者。

var string = "Hello, world!"var firstChar = string[0] // Throws error

错误:“下标”不可用:不能用Int下标字符串,有关讨论,请参阅文档注释


绝地无双
浏览 2145回答 3
3回答

开心每一天1111

注意:请看利奥·达布斯的回答对于SWIFT 4的适当实现。SWIFT 4这个Substring在SWIFT 4中引入了类型,以便通过与原始字符串共享存储,使子字符串更快、更高效,因此下标函数应该返回该类型。试试看这里extension&nbsp;String&nbsp;{ &nbsp;&nbsp;subscript&nbsp;(i:&nbsp;Int)&nbsp;->&nbsp;Character&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[index(startIndex,&nbsp;offsetBy:&nbsp;i)] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;CountableRange<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[start&nbsp;..<&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;CountableClosedRange<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[start&nbsp;...&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;CountablePartialRangeFrom<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(endIndex,&nbsp;offsetBy:&nbsp;-1) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[start&nbsp;...&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;PartialRangeThrough<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[startIndex&nbsp;...&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;PartialRangeUpTo<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[startIndex&nbsp;..<&nbsp;end] &nbsp;&nbsp;}}extension&nbsp;Substring&nbsp;{ &nbsp;&nbsp;subscript&nbsp;(i:&nbsp;Int)&nbsp;->&nbsp;Character&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[index(startIndex,&nbsp;offsetBy:&nbsp;i)] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;CountableRange<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[start&nbsp;..<&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;CountableClosedRange<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[start&nbsp;...&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;CountablePartialRangeFrom<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(endIndex,&nbsp;offsetBy:&nbsp;-1) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[start&nbsp;...&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;PartialRangeThrough<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[startIndex&nbsp;...&nbsp;end] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(bounds:&nbsp;PartialRangeUpTo<Int>)&nbsp;->&nbsp;Substring&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;bounds.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[startIndex&nbsp;..<&nbsp;end] &nbsp;&nbsp;}}若要转换Substring变成String,你可以简单地做String(string[0..2]),但只有当您计划保留子字符串时,才应该这样做。否则,将其保持为Substring.如果有人能想出一个很好的方法把这两个扩展合并成一个,那就太好了。我试着扩展StringProtocol没有成功,因为index方法不存在。&nbsp;SWIFT 3:extension&nbsp;String&nbsp;{ &nbsp;&nbsp;subscript&nbsp;(i:&nbsp;Int)&nbsp;->&nbsp;Character&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[index(startIndex,&nbsp;offsetBy:&nbsp;i)] &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(i:&nbsp;Int)&nbsp;->&nbsp;String&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;String(self[i]&nbsp;as&nbsp;Character) &nbsp;&nbsp;} &nbsp;&nbsp;subscript&nbsp;(r:&nbsp;Range<Int>)&nbsp;->&nbsp;String&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;r.lowerBound) &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;end&nbsp;=&nbsp;index(startIndex,&nbsp;offsetBy:&nbsp;r.upperBound) &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self[Range(start&nbsp;..<&nbsp;end)] &nbsp;&nbsp;}}&nbsp;为什么这不是内置的?Apple提供了以下解释(在这里发现的):带有整数的子脚本字符串不可用。“i字符串中的TH字符在不同的库和系统组件中有不同的解释。正确的解释应该根据用例和所涉及的api选择,所以String不能用整数订阅。SWIFT提供了几种访问字符串中存储的字符数据的不同方法。String.utf8是字符串中UTF-8代码单元的集合。将字符串转换为UTF-8时使用此API。大多数POSIX API以UTF-8代码单元处理字符串。String.utf16是字符串中UTF-16代码单元的集合。大多数Cocoa和CocoaTouch API根据UTF-16代码单元处理字符串。例如,NSRange与.连用NSAttributedString和NSRegularExpression以UTF-16码单元存储子字符串偏移量和长度。String.unicodeScalars是Unicode标量的集合。当您执行字符数据的低级别操作时,请使用此API。String.characters是一组扩展的字素聚类,它是用户感知字符的近似。请注意,在处理包含可读的文本的字符串时,应尽可能避免逐字符处理。例如,使用高级别的对地区敏感的Unicode算法,String.localizedStandardCompare(),String.localizedLowercaseString,&nbsp;String.localizedStandardRangeOfString()等。
打开App,查看更多内容
随时随地看视频慕课网APP