如何在c#中为字符串添加单引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12107437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add single quotes to string in c#?
提问by user1619151
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] + "'";
回答by Tom Gullen
Not sure I fully understand, but there are two ways.
不确定我是否完全理解,但有两种方法。
Output: ".","."
输出: ".","."
var s1 = @"""."","".""";
var s2 = "\".\",\".\"";
回答by Habib
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] + "'";
回答by Vishal Suthar
I just worked with your code..it works just fine..
我刚刚使用了你的代码..它工作得很好..
Debugger will show: "'val1','val2'"
调试器将显示: "'val1','val2'"
Actual Value : 'val1','val2'
实际价值 : 'val1','val2'
From the debugger point you will see "'val1','val2'"but actually it happens for every stringvalues. but actually when you prints the value in a page you will see the actual value as 'val1','val2'
从调试器的角度你会看到,"'val1','val2'"但实际上它发生在每个string值上。但实际上,当您在页面中打印值时,您会看到实际值'val1','val2'
EDIT:
编辑:
For sending those values to javascript what you can do is just set those values in Hidden fieldsand then fetch those value from that using document.getElementById('hidden_field').
要将这些值发送到 javascript,您可以做的只是设置这些值Hidden fields,然后使用document.getElementById('hidden_field').
回答by Priyank Thakkar
The best way to encapsualte escape sequences and special characters within a string is the use of webatim character.
在字符串中封装转义序列和特殊字符的最佳方法是使用 webatim 字符。
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.
您必须在字符串开始之前和 "" 字符之外放置的 @ 字符。

