Click here to Skip to main content
15,887,676 members
Articles / Programming Languages / C#
Tip/Trick

Add Non Escape Double Quotes to a String in .NET

Rate me:
Please Sign up or sign in to vote.
2.83/5 (6 votes)
24 Jun 2015CPOL 22.7K   2   5
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 []).

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

History

  • 24-Jun-2015: Created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Frank Kerrigan

Currently developing Insurance systems with SQL Server, ASP.NET, C#, ADO for a company in Glasgow Scotland. Very keen on OOP and NUNIT testing. Been in IT forever (20 years) in mix of development and supporting applications / servers. Worked for companies big and small and enjoyed both.

Developed in (newest first) : C#, Progress 4GL, ASP.NET, SQL TSQL, HTML, VB.NET, ASP, VB, VBscript, JavaScript, Oracle PSQL, perl, Access v1-2000, sybase/informi, Pic Controllers, 6502 (ask your dad).

Msc .Net Development Evenings www.gcu.ac.uk
MCAD Passed
MCP C# ASP.NET Web Applications
MCP SQL Server 2000
HND Computing
OND / HNC Electrical Engineering,

Comments and Discussions

 
QuestionMy vote of 1 Pin
jimmson24-Jun-15 4:42
jimmson24-Jun-15 4:42 
So how exactly StringBuilder helps you with this?
GeneralMy vote of 1 Pin
Pascal-7824-Jun-15 4:28
professionalPascal-7824-Jun-15 4:28 
GeneralYour StringBuilder is useless Pin
Pascal-7824-Jun-15 4:01
professionalPascal-7824-Jun-15 4:01 
GeneralRe: Your StringBuilder is useless Pin
bobrien10024-Jun-15 5:24
bobrien10024-Jun-15 5:24 
GeneralRe: Your StringBuilder is useless Pin
Pascal-7824-Jun-15 6:56
professionalPascal-7824-Jun-15 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.