Swift : How to get the string before a certain character?

前端 未结 11 1901
北恋
北恋 2020-12-24 01:45

How do I get the string before a certain character in swift? The code below is how I did it in Objective C, but can\'t seem to perform the same task in Swift. Any tips or su

11条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 02:31

    You can use rangeOfString, but it returns a Range type, not a NSRange:

    let string = "hello Swift"
    if let range = string.rangeOfString("Swift") {
        print(string.substringToIndex(range.startIndex))
    }
    

提交回复
热议问题