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

Here is a way to do it:

var str = "str.str"

if let dotRange = str.rangeOfString(".") {
    str.removeRange(dotRange.startIndex..<str.endIndex)
}

Update
In Swift 3 it is:

var str = "str.str"

if let dotRange = str.range(of: ".") {
  str.removeSubrange(dotRange.lowerBound..<str.endIndex)
}

Leave a Comment