Strip double quotes from a string in .NET
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Removing double quotes from a string in .NET is simple once you decide exactly what "remove quotes" means. Sometimes you want to delete every quote character in the string, and sometimes you only want to remove a matching quote at the start and end. Choosing the right method matters because Replace, Trim, and regular expressions solve different problems.
Remove Every Double Quote with Replace
If the requirement is literally "delete every \" character anywhere in the string," use string.Replace.
Output:
This is the clearest and most efficient answer for the all-quotes case. It also reads well when someone comes back to the code later.
One thing to remember is that strings in .NET are immutable. Replace does not change the original string in place. It returns a new string, so you must store the result.
Remove Only Surrounding Quotes with Trim
If the string may be wrapped in quotes and you want to remove only those outer characters, use Trim('"').
This prints:
That method is useful for parsing CSV-like or configuration-like values where the outer quotes are syntactic decoration.
Be careful, though: Trim('"') removes quote characters from both ends until it hits a different character. It does not verify that the quotes are a matched pair. For example, it will remove leading quotes even if the trailing quote is missing.
Check for a Proper Wrapped String
If you only want to strip the first and last characters when both are quotes, write that rule explicitly.
This approach is more precise than Trim because it preserves malformed or partially quoted data instead of silently reshaping it.
Use Regular Expressions Only for Pattern-Based Rules
If the quoting rule is more complicated, such as removing only quotes that surround a token with optional whitespace, regular expressions can help.
This is powerful, but it is usually not the first tool to reach for. If Replace or a simple boundary check solves the problem, that code will be easier to maintain.
Choose Based on the Actual Requirement
These are three different operations:
- '
Replace("\"", "")removes every quote character.' - '
Trim('"')removes quote characters from both ends.' - a boundary check removes only one opening and one closing quote when both are present.
A lot of bugs happen because developers know how to remove quotes, but they pick the wrong meaning of "remove." That is especially common when data comes from JSON fragments, CSV fields, or user input that may already be partially sanitized.
Escaped Quotes and Serialization
Another source of confusion is escaped quotes. A C# string literal like "\\\"hello\\\"" is source-code syntax, not the runtime string itself. By the time your code runs, the in-memory value already contains ordinary quote characters.
That means your removal logic should be written for the runtime data, not for how the string looked in source code. If the string came from JSON or another serialization format, make sure you are not solving the wrong problem. Sometimes the right fix is to deserialize correctly rather than manually deleting quote characters.
Common Pitfalls
One common mistake is using Trim('"') when the requirement is to remove every quote in the string. Trim only works at the edges, so embedded quotes remain untouched.
Another mistake is using Replace when the real requirement is to remove only the outer wrapper. That can corrupt valid data such as He said "hello" by deleting meaningful internal quotes.
Developers also sometimes forget that strings are immutable. Calling input.Replace("\"", "") without assigning the result does nothing useful.
Finally, be careful with malformed input. If you need to preserve bad data for validation or error reporting, do not blindly trim away leading or trailing quote characters without checking that the string is truly wrapped.
Summary
- Use
Replace("\"", "")when you want to remove every double quote character. - Use
Trim('"')when you want to remove quote characters from both ends. - Use an explicit boundary check when you want to remove only a proper wrapping pair.
- Prefer simple string methods over regular expressions unless the matching rule is genuinely pattern-based.
- Decide what "strip quotes" means before choosing the implementation.
Related reading
- Sum of digits in C
- Support Vector Machine library for C
- swagger .net core API ambiguous HTTP method for Action Error
- Switch case on type c
- Synchronous I/O within an async/await-based Windows Service
- System.BadImageFormatException Could not load file or assembly from installutil.exe
- System.Diagnostics.Debug.WriteLine in production code
- System.MissingMethodException Method not found?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.