What's the simplest algorithm to escape a single character?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The simplest character escaping algorithm is prefix-based escaping: prepend a designated escape character (usually backslash \) before any character that has special meaning. To escape the string, scan each character — if it is a special character or the escape character itself, insert the escape prefix before it. To unescape, scan for the escape prefix and remove it while keeping the following character as literal. This two-pass algorithm handles any set of special characters.
The Basic Algorithm
Why Escape the Escape Character?
The escape character itself must be escaped, otherwise you cannot represent it literally:
Without this rule, \n would be ambiguous — is it an escaped n (the letter) or a literal backslash followed by n?
Language-Specific Examples
Python
JavaScript
C / C++
SQL
URL Encoding (Percent Encoding)
A different escaping scheme where special characters are replaced with %XX (hex):
HTML Entity Escaping
Replaces characters with named or numeric entities:
Comparison of Escaping Strategies
| Strategy | Escape Format | Example | ||
| Backslash prefix | `` + char | \" for " | ||
| Doubling | char + char | '' for ' (SQL) | ||
| Percent encoding | % + hex | %20 for space | ||
| HTML entities | &name; or &#num; | & for & | ||
| Caret (CMD) | ^ + char | ^ | for| |
Common Pitfalls
- Forgetting to escape the escape character: If
\is the escape character but is not itself escaped, the stringa\bis ambiguous (isbbeing escaped or is this a literal backslash?). Always escape the escape character first. - Double escaping: Escaping an already-escaped string produces
\\\"instead of\". Track whether data is already escaped and never escape twice. This is a frequent bug when passing data through multiple layers (database, API, template). - Using string escaping instead of parameterized queries for SQL: Manual SQL escaping is fragile and language-dependent. Always use parameterized queries (
?or%splaceholders) to prevent SQL injection. - Escaping for the wrong context: HTML escaping (
<) does not protect against JavaScript injection inonclickattributes. URL encoding does not help in HTML body text. Each context (HTML, JS, URL, SQL, shell) requires its own escaping rules. - Not unescaping on the receiving end: If you escape data before sending, the receiver must unescape it. Displaying
Hello\ Worldto a user because you forgot to unescape is a common presentation bug.
Summary
- The simplest escaping algorithm prefixes special characters with an escape character (usually
\) - Always escape the escape character itself to avoid ambiguity
- Use language-built-in functions (
re.escape,html.escape,JSON.stringify,encodeURIComponent) instead of manual implementations - Different contexts (HTML, SQL, URL, shell) require different escaping rules — never mix them
- For SQL, always use parameterized queries instead of manual character escaping
Related reading
- What's the simplest algorithm/solution for a single pair shortest path through a real-weighted undirected graph?
- What's the standard algorithm for syncing two lists of related objects?
- What's the Time Complexity of Average Regex algorithms?
- What's the time complexity of this algorithm for Palindrome Partitioning?
- What's time complexity of this algorithm for finding all combinations?
- When are bitwise operations appropriate
- When do floors and ceilings matter while solving recurrences?
- When do you exactly use consensus algorithm in distributed system?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.