.NET
string manipulation
C#
double quotes
programming tips

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.

Browse interview questions

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.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        string input = "\"hello\", \"world\"";
8        string result = input.Replace("\"", "");
9
10        Console.WriteLine(input);
11        Console.WriteLine(result);
12    }
13}

Output:

text
"hello", "world"
hello, world

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('"').

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        string input = "\"hello world\"";
8        string result = input.Trim('"');
9
10        Console.WriteLine(result);
11    }
12}

This prints:

text
hello world

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.

csharp
1using System;
2
3class Program
4{
5    static string StripWrappingQuotes(string input)
6    {
7        if (string.IsNullOrEmpty(input))
8        {
9            return input;
10        }
11
12        if (input.Length >= 2 && input[0] == '"' && input[^1] == '"')
13        {
14            return input.Substring(1, input.Length - 2);
15        }
16
17        return input;
18    }
19
20    static void Main()
21    {
22        Console.WriteLine(StripWrappingQuotes("\"value\""));
23        Console.WriteLine(StripWrappingQuotes("\"broken"));
24        Console.WriteLine(StripWrappingQuotes("plain"));
25    }
26}

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.

csharp
1using System;
2using System.Text.RegularExpressions;
3
4class Program
5{
6    static void Main()
7    {
8        string input = "   \"example\"   ";
9        string result = Regex.Replace(input, "^\\s*\"(.*)\"\\s*$", "$1");
10
11        Console.WriteLine(result);
12    }
13}

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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.