Changing a character 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
Changing one character in a string is a simple task conceptually, but the implementation depends on whether the language treats strings as mutable. In many mainstream languages, strings are immutable, which means you do not modify the original string in place; you build a new string with the changed character.
The Core Idea: Strings Are Often Immutable
In languages such as Python, Java, JavaScript, and C#, strings are immutable. That means this idea is usually wrong:
- find character at index
i - overwrite it inside the same string object
Instead, the normal pattern is:
- take the part before the index
- insert the replacement character
- append the rest of the string
That produces a new string value.
Python Example
Python strings are immutable, so slicing is the standard approach:
If you need to make many changes, converting once to a list of characters can be more efficient:
This avoids repeated string reconstruction when many edits are required.
JavaScript Example
JavaScript strings are also immutable:
Just like in Python, the original string is unchanged and a new one is returned.
Java Example
In Java, String is immutable, so substring composition is a standard pattern:
If you are making many modifications, StringBuilder is often a better tool:
This is a good reminder that sometimes the right answer is not “change the string,” but “use a mutable string-like structure.”
C# Example
C# strings are immutable too:
For multiple edits, converting to a character array can be clearer:
Validate the Index
Regardless of language, you should validate that the target index is inside the string bounds. A simple defensive helper in Python looks like this:
This avoids subtle bugs such as empty replacements or accidental multi-character insertion when the caller meant to replace exactly one character.
When Not to Treat It as a Single-Character Problem
Sometimes the operation is really:
- replace every occurrence of a character
- replace a substring
- perform many edits efficiently
In those cases, language-specific replace functions or mutable builders are usually better than repeating single-index logic.
The key is to match the tool to the real operation instead of forcing everything through one-character replacement code.
Common Pitfalls
- Assuming strings are mutable and trying to assign directly into them.
- Forgetting to validate the index and causing out-of-range errors.
- Allowing a multi-character replacement when the intent was to replace exactly one character.
- Rebuilding very large strings repeatedly when a mutable buffer would be more efficient.
- Solving a global replace problem with single-character index logic.
Summary
- In many languages, changing a character in a string really means creating a new string.
- Slicing and concatenation are the usual one-off solution.
- For repeated edits, use a mutable representation such as a list, character array, or string builder.
- Always validate the target index and replacement size.
- Choose the operation based on whether you need one replacement, many replacements, or full substring manipulation.

