What is the more elegant way to remove all characters after specific character in the String object in Swift

后端 未结 4 768
既然无缘
既然无缘 2021-02-01 15:06

What is the more elegant way to remove all characters after specific character in the String object in Swift?

Suppose that I have the following string:

4条回答
  •  礼貌的吻别
    2021-02-01 15:21

    Here is a way to do it:

    var str = "str.str"
    
    if let dotRange = str.rangeOfString(".") {
        str.removeRange(dotRange.startIndex..

    Update In Swift 3 it is:

    var str = "str.str"
    
    if let dotRange = str.range(of: ".") {
      str.removeSubrange(dotRange.lowerBound..

提交回复
热议问题