Any way to replace characters on Swift String?

Posted by Rohit Kumar | Updated on

If you want to replace characters on Swift String? You can use replacingOccurrences()


And as noted by @cprcrack below, the options and range parameters are optional, so if you don’t want to specify string comparison options or a range to do the replacement within, you only need the following.

Or, if the data is in a specific format like this, where you’re just replacing separation characters, you can use components() to break the string into and array, and then you can use the join() function to put them back to together with a specified separator.

Or if you’re looking for a more Swifty solution that doesn’t utilize API from NSString, you could use this.

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers