Swift - How to replace characters in a String?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Replacing characters in a Swift String can mean a few different things: replacing all occurrences of a substring, replacing one specific character at a known position, or doing pattern-based replacement with a regular expression. Swift supports all of these, but strings are Unicode-correct collections, so the API is more careful than simple integer indexing in some other languages.
Replace All Matching Text
The most common tool is replacingOccurrences(of:with:).
This returns a new string. It does not mutate the original value.
That makes it ideal for replacing all matching characters or substrings in one call.
Replace One Character at a Specific Position
If you need to change only one known character, use string indices and replaceSubrange.
Swift does not let you write text[1] = "a" because String uses variable-width Unicode encoding. Using proper string indices keeps the operation safe.
Use Regular Expressions for Pattern-Based Replacement
For more complex patterns, Foundation regular expressions are useful.
This is a good fit when the replacement target is defined by a pattern rather than a literal character.
Be Clear About Mutation Versus Return Value
A common Swift mistake is forgetting that many string replacement APIs return a new string.
You must assign the result:
If you want to mutate a String in place, use var plus range-based replacement methods.
Replace Characters with Manual Logic
If the rule depends on each character, build a new string manually.
This is useful when the transformation is more custom than a simple literal replacement.
Unicode and Indexing Matter
Swift strings are Unicode-aware, which is the right behavior, but it means you should avoid treating them like arrays of bytes or fixed-width characters.
For example:
- '
String.Indexis safer than integer offsets' - one user-visible character may not equal one byte
- replacing by position should use indices derived from the string itself
That matters especially when strings may contain emoji, accented characters, or combined glyphs.
Common Pitfalls
The most common issue is forgetting to store the result of replacingOccurrences. The original string stays unchanged unless you assign the new value.
Another problem is trying to index strings with integers as if they were simple arrays. Swift intentionally avoids that because Unicode-safe indexing is more complex.
Developers also sometimes reach for regular expressions when a literal replacement is enough. replacingOccurrences(of:with:) is simpler and clearer when the target text is fixed.
Finally, be careful when replacing a single character by position. Always derive the index from the actual string rather than assuming a certain offset maps cleanly to a visible character.
Summary
- Use
replacingOccurrences(of:with:)for simple global replacement. - Use
replaceSubrangewith a validString.Indexfor one targeted replacement. - Use regular expressions when the replacement target is pattern-based.
- Remember that many Swift string replacement methods return a new string.
- Treat Swift strings as Unicode-aware values, not as integer-indexed byte arrays.

