Click here to Skip to main content
15,903,747 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.9K   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 
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 
I am not telling StringBuilder is useless, but in his example, it uses the StringBuilder purposes only for adding the last part ("= @NAME")

StringBuilder is useful in multiple concatenation of string because it will not create a temporary string object at each step.
for example :
- in 'a' + "bcd" + 'e'

1) 'a' will be stringify to "a"
2) "bcd" will be append to "a" in a new string "abcd"
3) 'e' will be stringify to "e"
4) new string will be created with "abcde"

So you will have 4 strings created and 3 are only temporary

With a correct use of StringBuilder
sb.Append('a');
sb.Append("bcd");
sb.Append('e');
no temporary string is created (you still may have reallocating array in StringBuilder if it is not initialized with enough capacity but it is an other story).

If you prefer, he misused the StringBuilder.

And What do you expect of '0' + '0' + "abcd" ?

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.