最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add single quotes to string in c#? - Stack Overflow

programmeradmin3浏览0评论

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] + "'";

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] + "'";
Share Improve this question edited Aug 24, 2012 at 10:17 Rup 34.4k9 gold badges86 silver badges118 bronze badges asked Aug 24, 2012 at 10:15 user1619151user1619151 591 gold badge2 silver badges6 bronze badges 8
  • 1 Have you tried using string.Format for building the strings? It could make your code a lot easier to read. – Richard Ev Commented Aug 24, 2012 at 10:17
  • 7 Maybe dumb question: are you sure the " are actually part of your string, and not just displayed in the debugger? – Rawling Commented Aug 24, 2012 at 10:17
  • Are you sure the the " is not exist after the split ? – Aristos Commented Aug 24, 2012 at 10:21
  • Your code works as is. Of course I assume the values in names doesn't contain a single quote. Right? – Steve Commented Aug 24, 2012 at 10:23
  • Rawling is right... If you are newbie for Visual Studio, might be u were trying to see the values during debugging. Don't worry your actual string won't contain double quotes "" – PawanS Commented Aug 24, 2012 at 10:27
 |  Show 3 more comments

4 Answers 4

Reset to default 8

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] + "'";

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

Output: ".","."

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

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.

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

发布评论

评论列表(0)

  1. 暂无评论