user1619151
user1619151

Reputation: 59

how to add single quotes to string in c#?

I am adding mutliple values to single string , all the values should be like in this format '','' but I am getting "'',''" instead.

How can I remove these double qoutes? Here's the code I'm using:

string one = "\'"+ names[0, 0] +"\'"+","+"\'" + names[1, 0]+"\'";
string[] splitted = one.Split('\'');
string ones = "'" + splitted[1] + "'" +","+ "'" + splitted[3] + "'";

Upvotes: 4

Views: 49337

Answers (4)

Priyank Thakkar
Priyank Thakkar

Reputation: 4852

The best way to encapsualte escape sequences and special characters within a string is the use of webatim character.

e.g.

String escSeq = "C:\\MyDocuments\\Movie";

can also be stored as:

string escSeq = @"C:\MyDocumens\Movie";

The @ character you must put before the string starts and outside the "" characters.

Upvotes: 3

Vishal Suthar
Vishal Suthar

Reputation: 17183

I just worked with your code..it works just fine..

Debugger will show: "'val1','val2'"

Actual Value : 'val1','val2'

From the debugger point you will see "'val1','val2'" but actually it happens for every string values. but actually when you prints the value in a page you will see the actual value as 'val1','val2'

EDIT:

For sending those values to javascript what you can do is just set those values in Hidden fields and then fetch those value from that using document.getElementById('hidden_field').

Upvotes: 0

Habib
Habib

Reputation: 223187

You don't have to escape single quote like "\'" instead you can simply use "'", so you code should be:

string one = "'" + names[0, 0] + "'" + "," + "'" + names[1, 0] + "'";
string[] splitted = one.Split('\'');
string ones = "'" + splitted[1] + "'" + "," + "'" + splitted[3] + "'";

Upvotes: 8

Tom Gullen
Tom Gullen

Reputation: 61729

Not sure I fully understand, but there are two ways.

Output: ".","."

var s1 = @"""."","".""";
var s2 = "\".\",\".\"";

Upvotes: 3

Related Questions