swift - 在 Swift 3.0 中循环遍历子字符串

标签 swift string loops substring swift-playground

在 Swift 3.0 中循环遍历子字符串的最佳方法是什么?

var start = s.startIndex
var end = s.index(s.endIndex, offsetBy: -10)
   for i in start...end {


   }

以下代码会引发错误:Type ClosedRange<String.Index> (aka ‘ClosedRange<String.CharacterView.Index>’) does not conform to Sequence protocol .

最佳答案

startendString.Index的类型,不能在for循环中使用;相反,您可以获得以下范围内的 substring:

var start = s.startIndex
var end = s.index(s.endIndex, offsetBy: -10)
let range = Range(uncheckedBounds: (lower: start, upper: end))
let subString = s.substring(with: range)

关于swift - 在 Swift 3.0 中循环遍历子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662298/

相关文章:

c - 如何比较c中字符串的某些部分?

jquery - 每 10 秒轮换一次背景图像 url

ios - 在动态值 TableView 中使用 performSeguewithIdentifier 函数

ios - 参数类型 'customClass.Type' 不符合预期类型 'NSItemProviderWriting'

ruby - 如何替换字符串中的特定字符以及紧邻的下一个字符

r - 循环创建错误数量的图

c++ - 控制结构让我失望

ios - 当我们点击它时,如何更改添加到表格 View 单元格上的按钮背景颜色?

ios - 从 Swift 函数中的异步调用返回数据

c - C中的字符串分隔和格式化