You are on the right track, but here
str.removeSubrange(dotRange.lowerBound..<str.startIndex)
the range bounds are in the wrong order. It should be
var str = "asderwt.qwertyu.zxcvbbnnhg"
if let dotRange = str.range(of: ".") {
str.removeSubrange(str.startIndex..<dotRange.lowerBound) // <-- HERE
print("Result: \(str)") // Result: .qwertyu.zxcvbbnnhg
}
You can also use a “partial range”
if let dotRange = str.range(of: ".") {
str.removeSubrange(..<dotRange.lowerBound)
print("Result: \(str)") // Result: .qwertyu.zxcvbbnnhg
}
Or with firstIndex(of:)
instead of range(of:)
:
if let dotIndex = str.firstIndex(of: ".") {
str.removeSubrange(..<dotIndex)
print("Result: \(str)") // Result: .qwertyu.zxcvbbnnhg
}