65.9K
CodeProject is changing. Read more.
Home

Add Non Escape Double Quotes to a String in .NET

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.83/5 (6 votes)

Jun 24, 2015

CPOL
viewsIcon

23824

Adding double quotes to string variable is easy with StringBuilder in .NET.

Introduction

I see several posts on a lot of sites with people trying to create strings with embedded double quotes. This can be easily achieved with string builder in .NET.

Using the Code

The trick is to use a StringBuilder to and wrap the double quote within two single quotes.

The example below builds a string fragment for building a SQL for Oracle with uses double quotes to surround field names (TSQL uses []).

StringBuilder sb = new StringBuilder();
            var col = "Name Col";
            sb.Append('"' + col + '"');
            sb.Append(" = @NAME ");

History

  • 24-Jun-2015: Created