代码之家  ›  专栏  ›  技术社区  ›  Evgeniy Kleban

特定索引后的Swift字符串[重复]

  •  -1
  • Evgeniy Kleban  · 技术社区  · 8 年前

    我想知道如何在特定字符后获得字符串的其余部分。

    我查过了 link

    但没有找到答案。

    考虑以下代码:

    let fullString = "Blue Sky"
    let spaceIndex = fullName.index(of: " ")!
    

    我知道我可以在这样之前得到第一个字符串:

    let firstString = fullString[fullString.startIndex..<spaceIndex] // "Blue”
    

    let firstString = fullString[fullString.startIndex..>spaceIndex] // "Blue” 不工作。

    3 回复  |  直到 8 年前
        1
  •  5
  •   RajeshKumar R    7 年前

    你可以把句子分成一组单词,然后得到最后一个单词。试试这个。您可以用任何字符或字符串替换“”。

    let fullString = "Blue Sky"
    print(fullString.components(separatedBy: " ").last)//Sky
    

    if let index = fullString.firstIndex(of: " ") {
        print(fullString[index...])//Sky
    }
    
        2
  •  2
  •   matiastofteby    8 年前

     let fullString = "Blue Sky"    
     let splitString = fullString.components(separatedBy: " ")
    
     print("Part before space: \(splitString[0])") // Part before space: Blue
     print("Part after space: \(splitString[1])") // Part after space: Sky
    
        3
  •  2
  •   Charles Srstka    8 年前

    没有这样的操作员 ..>

    然而,你 可以 使用 ..< fullString[spaceIndex..<fullString.endIndex] .

    或者,在Swift 4中,您可以: fullString[spaceIndex...]