【发布时间】:2018-03-07 04:27:41
【问题描述】:
这是使用官方 Swift4 文档中的示例代码
let greeting = "Hi there! It's nice to meet you! ????"
let endOfSentence = greeting.index(of: "!")!
let firstSentence = greeting[...endOfSentence]
// firstSentence == "Hi there!"
但是让我们说let greeting = "Hello there world!"
我想只检索这句话中的第二个单词(子字符串)?所以我只想要“那里”这个词。
我尝试过使用“世界!”作为一个论点
let endOfSentence = greeting.index(of: "world!")! 但 Swift 4 Playground 不喜欢这样。它期待 'Character' 而我的论点是一个字符串。
那么我怎样才能得到一个非常精确的子范围的子字符串呢?或者在一个句子中获取第 n 个单词以供将来更多地使用?
【问题讨论】: