Replace occurrences of a character in a String
Replacing a certain occurrence of a character in a string, for example a space(” “) is simple in Swift. Simply use the code below and substitute stringByReplacingOccurencesOfString(” “, withString: “+”) with the character you want to replace with a different character.
let myString: String = "This is my string" let newString = myString.replacingOccurrences(of: " ", with: "+") print(myString) print(newString)
This will output the following:
This is my string This+is+my+string
Easy!