我正在尝试提取一个字符串中用引号括起来的部分,例如在"Rouge One" is an awesome movie
中,我想要提取Rouge One。
到目前为止,我只能做到这一步,但无法确定下一步该怎么做:我创建了文本的一个副本,以便我可以删除第一个引号,从而获取第二个引号的索引。
if text.contains("\"") {
guard let firstQuoteMarkIndex = text.range(of: "\"") else {return}
var textCopy = text
let textWithoutFirstQuoteMark = textCopy.replacingCharacters(in: firstQuoteMarkIndex, with: "")
let secondQuoteMarkIndex = textCopy.range(of: "\"")
let stringBetweenQuotes = text.substring(with: Range(start: firstQuoteMarkIndex, end: secondQuoteMarkIndex))
}
"a"b"
这样的字符串呢?两个都应该匹配吗? - jtbandesr2.lowerBound
时,您忘记了对r2
进行解包。正确的写法应该是r2!.lowerBound
。虽然我不明白为什么不能使用r2?.lowerBound
。 - mfaanir1
和r2
都被赋予了可选绑定。如果第二个范围是nil
,那么if块将不会被执行。 - Martin R