How about this:
extension String { func removeCharsFromEnd(count:Int) -> String{ let stringLength = countElements(self) let substringIndex = (stringLength < count) ? 0 : stringLength - count return self.substringToIndex(advance(self.startIndex, substringIndex)) } func length() -> Int { return countElements(self) } }
Test:
var deviceName:String = "Mike Iphone" let newName = deviceName.removeCharsFromEnd(" Iphone".length())
But if you want the replace method to use stringByReplacingOccurrencesOfString as @Kirsteins :
let newName2 = deviceName.stringByReplacingOccurrencesOfString( " Iphone", withString: "", options: .allZeros, // or just nil range: nil)
source share