String Manipulation
Character Replacement
Programming
Coding
Text Processing

Replacing instances of 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

Replacing characters in a string is one of the most common text-processing tasks in programming. The core idea is simple, but the details vary depending on whether you want a literal replacement, a conditional replacement, or a regex-driven transformation.

Strings Are Usually Immutable

In most mainstream languages, strings are immutable. That means replacement does not modify the existing string in place. Instead, it creates a new string with the requested changes.

Python example:

python
1text = "banana"
2updated = text.replace("a", "o")
3
4print(text)     # banana
5print(updated)  # bonono

That original text value stays unchanged. The replacement result is a new object.

Literal Character Replacement

For plain one-character replacement, most languages provide a built-in method.

Python:

python
text = "a-b-c-d"
print(text.replace("-", "_"))

JavaScript:

javascript
const text = "a-b-c-d";
console.log(text.replaceAll("-", "_"));

C#:

csharp
string text = "a-b-c-d";
string updated = text.Replace('-', '_');
Console.WriteLine(updated);

If your use case is truly literal, the built-in string replacement method is almost always the right first choice.

Replace Only Some Occurrences

Sometimes you want to replace only the first few matches. Python supports this directly:

python
text = "a-a-a-a"
print(text.replace("a", "x", 2))  # x-x-a-a

If the language you are using does not expose a count argument, you may need a regex replacement or manual string construction instead.

Conditional Replacement

If the replacement depends on context, a simple literal method may not be enough. In that case, iterate through the characters and build a new result.

python
1text = "abracadabra"
2
3result = "".join(
4    "A" if ch == "a" and index % 2 == 0 else ch
5    for index, ch in enumerate(text)
6)
7
8print(result)

This is slower than a direct built-in replacement, but it gives full control over when the character changes.

Regular Expressions Are a Different Tool

Regex-based replacement is useful when the thing you are replacing is defined by a pattern rather than one literal character.

python
1import re
2
3text = "room 101, room 202"
4updated = re.sub(r"\\d", "#", text)
5print(updated)  # room ###, room ###

This is powerful, but it also changes the semantics. Once you move to regex replacement, special pattern characters matter, escaping matters, and performance can change.

Unicode and Normalization Can Matter

Character replacement gets trickier with Unicode. Two visually identical characters are not always stored the same way. For example, an accented letter may exist either as one composed code point or as a base letter plus a combining mark.

If a replacement seems to "miss" some characters, normalization may be the missing step rather than a bug in the replacement function.

Common Pitfalls

The biggest pitfall is forgetting that strings are immutable. Calling a replacement method and then ignoring the return value means nothing changes.

Another common mistake is using a regex-based replacement when a literal replacement was intended. That can create surprising behavior if the search pattern contains regex metacharacters.

Developers also overlook Unicode details. If the source text can contain accented characters or mixed encodings, a visible character may not correspond to one simple internal code-point pattern.

Summary

  • Character replacement usually returns a new string rather than changing the original one.
  • Use the built-in literal replacement method when the target is a specific known character.
  • Use counted replacement or manual logic when only some occurrences should change.
  • Use regex replacement only when the target is really a pattern.
  • Remember that Unicode representation can affect whether a character match succeeds.

Course illustration
Course illustration

All Rights Reserved.