我试着从字符串中取出引号中的部分,即在"Rouge One" is an awesome movie中,我想提取胭脂一号。
这就是我到目前为止所掌握的,但不知道从这里走到哪里:我创建了一个文本副本,以便删除第一个引号,以便获得第二个引号的索引。

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))
}

最佳答案

var rouge = "\"Rouge One\" is an awesome movie"

var separated = rouge.components(separatedBy: "\"") // ["", "Rouge One", " is an awesome movie"]

separated.dropFirst().first

关于swift - 在字符串中的引号内查找字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41351393/

10-11 04:13