- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试检测字符串上的括号,例如: foo(bar)baz(blim)
并反转括号内的内容,但我超出了反弹范围我的实现:
func reverseInParentheses(inputString: String) -> String {
var tmpStr = inputString
var done = false
while !done {
if let lastIndexOfChar = tmpStr.lastIndex(of: "(") {
let startIndex = tmpStr.index(lastIndexOfChar, offsetBy:1)
if let index = tmpStr.firstIndex(of: ")") {
let range = startIndex..<index
let strToVerse = String(tmpStr[range])
let reversedStr = reverseStr(str: strToVerse)
tmpStr = tmpStr.replacingOccurrences(of: "(" + strToVerse + ")", with: reversedStr)
}
} else {
done = true
}
}
return tmpStr
}
如何在 startIndex
之后获取 tmpStr.firstIndex(of: ")")
你们知道如何做到这一点吗?
最佳答案
how can I get the
tmpStr.firstIndex(of: ")")
after thestartIndex
?
一种方法是在 startIndex
处“剪切”字符串,并获取后半部分。然后在子字符串上使用 firstIndex(of:)
。由于 Substring
只是从中剪切它们的原始字符串的“ View ”,因此 firstIndexOf
仍然返回原始字符串的索引。
let string = "foo(bar)baz(blim)"
if let lastIndexOfChar = string.lastIndex(of: "(") {
let startIndex = string.index(after: lastIndexOfChar)
let substring = string[startIndex..<string.endIndex] // cut off the first part of the string.
// now you have a "Substring" object
if let indexAfterOpenBracket = substring.firstIndex(of: ")") {
// prints "blim", showing that the index is indeed from the original string
print(string[startIndex..<indexAfterOpenBracket])
}
}
您可以将其编写为扩展:
extension StringProtocol {
func firstIndex(of char: Character, after index: Index) -> Index? {
let substring = self[index..<endIndex]
return substring.firstIndex(of: char)
}
}
现在,如果您在 reverseInParentheses
中调用 tmpStr.firstIndex(of: ")", after: startIndex)
,它应该可以工作。
关于ios - Swift:字符后的字符串firstIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65402215/
我有这门课: class ValueTimestamp { let value: Double let timestamp : Double init(value:Double, time
我看到了几个问题,但没有找到解决方案和原因 这里是结构体 struct MovementFormattedData { ... Other properties ... lazy var
FirstIndex 属性 返回在搜索字符串中匹配的位置。 object.FirstIndex object 参数总是一个 Match 对象。 说明 FirstIndex 属性使用从零起算
美好的一天 – 我有一个例行程序,它会搜索数万个文本文件以捕获相关文件名,从而剩下不到 10,000 个文件名。这些文件名为下一个例程提供了起点,以打开每个剩余的文本文件以搜索特定数据。每个文本文件可
从文档中它说 firstindex()查找集合的第一个索引。为什么不只使用 1?当它不是 1 时会出现什么情况? 最佳答案 第一个索引不一定是 1,因为 Julia 支持 custom indexin
我在 Xcode 10 上的 Swift 4 中有以下代码 var newCard = deck.randomElement()! deck.remove(at: de
当我在字典数组上使用 firstIndex 时,出现 Incorrect argument label in call (have 'where:', expected 'of:') 错误。 let
在遵循有关 SwiftUI 的 Apple 教程时,我发现了这一点: 1| var landmarkIndex: Int { 2| userData.landmarks.firstIndex(w
我不明白这个命令结构是如何工作的。除了 firstIndex 之外,所有这些似乎都有意义(在文档中;我实际上还没有调用该函数)。实际上,在我看来文档中好像有错字。 这是我在查找相关文档时似乎在每个地方
我试图将一个字符串拆分成一个字母数组,但将一些字母放在一起。 (例如,我试图将它们分成发音组)。因此,举例来说,所有“sh”组合将是数组中的一个值而不是两个。 使用 firstIndex 很容易在我知
我有两个 Book 数组 var tempArray = [Book]() var filteredArray = [Book]() 在哪里 struct Book: Codable, Equatab
摘自 Apple 的 The Swift Programming Language (Swift 4.2) 文档。 let greeting = "Hello, world!" let index =
这个问题在这里已经有了答案: Swift 4.2, String firstIndex() function error in Xcode playground (1 个回答) 关闭 4 年前。 您
我在读《The Swift Programming Language》Swift 4.2”和“子字符串”部分下的“字符串和字符”一章中,给出了以下代码作为示例: let greeting = "Hel
我是一名优秀的程序员,十分优秀!