Best way to replace multiple characters in a string?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The best approach to replacing multiple characters in a string depends on your language and use case. In Python, str.translate() with str.maketrans() is the fastest for character-level replacements. In JavaScript, use a regex character class with replace(). For multi-character substring replacements, chain .replace() calls or use a dictionary-based approach. Avoid repeated string concatenation in loops — it creates unnecessary intermediate strings.
Python
str.translate() — Fastest for Character Replacement
str.translate() is implemented in C and processes the entire string in one pass — significantly faster than chaining .replace().
Chained .replace()
Simple and readable for a few replacements, but each .replace() creates a new string and scans the entire text. For many replacements, use translate() instead.
Regular Expressions
Dictionary-Based Replacement
The regex approach processes the string in a single pass, making it faster for many replacements.
JavaScript
C#
Java
Go
Go's strings.NewReplacer is optimized internally — it builds a lookup structure and processes the string in one pass.
Performance Comparison (Python)
str.translate() is 5x faster than chained .replace() and 15x faster than regex for character-level replacements.
Common Pitfalls
- Chaining too many
.replace()calls: Each creates a new string and scans the full text. For more than 3-4 replacements, usetranslate()(Python),NewReplacer(Go), or regex. - Overlapping replacements:
"abc".replace("a", "b").replace("b", "c")produces"ccc", not"bca". The second replace acts on the output of the first. Use a single-pass approach (regex with dict ortranslate) to avoid this. - Regex special characters: Characters like
.,*,(,)have special meaning in regex. Escape them withre.escape()(Python) or\\when using them in patterns. - Immutable strings: In Python, Java, C#, Go, and JavaScript, strings are immutable. Each
.replace()creates a new string. UseStringBuilder(C#/Java) for many mutations in a loop. - Case sensitivity:
.replace()is case-sensitive by default. Usere.sub(pattern, replacement, text, flags=re.IGNORECASE)for case-insensitive replacement.
Summary
- Python:
str.translate()for character replacement, regex for complex patterns - JavaScript:
str.replace(/[chars]/g, "")with a regex character class - C#/Java: chained
.Replace()for few replacements,Regex.Replace()for many - Go:
strings.NewReplacer()for efficient multi-pair replacement - Use single-pass methods (translate, regex, NewReplacer) for best performance
- Avoid chained
.replace()for overlapping replacements — they interact unexpectedly
Related reading
- Best way to strip punctuation from a string
- Best way to strip punctuation from a string
- BestPractice - Transform first character of a string into lower case
- Binarization in Natural Language Processing
- Body Text extraction from websites e.g. extract only article heading and text not all text in site
- Can an algorithm detect sarcasm
- Can an author's unique literary style be used to identify him/her as the author of a text?
- Can anyone explain how to get BIDMach's Word2vec to work?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.