如何向变量内部的字符串添加双引号?

时间:2022-09-15 16:18:25

I have variable like:

我有这样的变量:

string title = string.empty;

My need is that whatever string is passed to it I have to display the content inside a div with in a double quotes. So I have written something like:

我的需要是,不管传递给它的字符串是什么,我都必须在div中显示包含双引号的内容。所以我写了这样的东西:

...
...
<div>"+ title +@"</div>
...
...

But how to add the double quotes here? So that it will display like :

但是如何在这里添加双引号呢?这样它就会显示:

"How to add double quotes"

15 个解决方案

#1


227  

You need to escape them by doubling them (verbatim string literal):

您需要通过加倍它们来转义它们(逐字字符串):

string str = @"""How to add doublequotes""";

Or with a normal string literal you escape them with a \:

或者用一个普通的字符串文字你用一个\:

string str = "\"How to add doublequotes\"";

#2


36  

So you are essentially asking how to store doublequotes within a string variable? Two solutions for that:

你问的是如何在字符串变量中存储双引号?两种解决方案:

var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";

In order to perhaps make it a bit more clear what happens:

为了更清楚地说明发生了什么:

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";

#3


11  

If I understand your question properly, maybe you can try this:

如果我能正确理解你的问题,也许你可以试试:

string title = string.Format("<div>\"{0}\"</div>", "some text");

#4


5  

Another Note:

另注:

    string path = @"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi"; 
    string hh = string.Format("\"{0}\"", path);
    Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");

The real value of hh as passed will be "H:\MOVIES\Battel SHIP\done-battleship-cd1.avi".

通过的hh的真正价值将是“H:\电影《巴氏战舰-战列舰-cd1.avi》”。

When needing double double literals use: @"H:\MOVIES\Battel SHIP\done-battleship-cd1.avi"; Instead of: @"H:\MOVIESBattel SHIP\done-battleship-cd1.avi"; Because the first literals is for the path name and then the second literals are for the double quotation marks

当需要双双字时,使用:@“H:\电影\Battel船\done- battles- cd1.avi”;而不是:@“H:\ \ done-battleship-cd1.avi MOVIESBattel船”;因为第一个常量是路径名,第二个常量是双引号

#5


5  

If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.

如果你需要经常这样做,并且希望代码更简洁,你可能需要一个扩展方法。

This is really obvious code, but still I think it can be useful to grab and make you save time.

这是非常明显的代码,但是我仍然认为它可以帮助您获取并节省时间。

  /// <summary>
    /// Put a string between double quotes.
    /// </summary>
    /// <param name="value">Value to be put between double quotes ex: foo</param>
    /// <returns>double quoted string ex: "foo"</returns>
    public static string AddDoubleQuotes(this string value)
    {
        return "\"" + value + "\"";
    }

Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.

然后可以在每个字符串上调用foo. adddoublequotes()或“foo”. adddoublequotes()。

Hope this help.

希望这个有帮助。

#6


2  

You could use &quot; instead of ". It will be displayed correctly by the browser.

您可以使用“而不是“。浏览器将正确地显示它。

#7


2  

You can use $.

你可以使用美元。

Interpolated Strings: Used to construct strings. An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations. This feature is available in C# 6 and later versions.

内插字符串:用于构造字符串。内插字符串看起来像一个包含内插表达式的模板字符串。内插字符串返回一个字符串,该字符串用其字符串表示替换其包含的内插表达式。这个特性可以在c# 6和后续版本中使用。

string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";

#8


1  

Use either

使用

&dquo;
<div>&dquo;"+ title +@"&dquo;</div>

or escape the double quote:

或者避免重复引用:

\"
<div>\""+ title +@"\"</div>

#9


1  

Using string interpolation with working example:

使用字符串插值与工作示例:

var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>";  //Note the order of the $@
Console.WriteLine (string1);

Output

输出

<div>"Title between quotes"</div>

#10


1  

string doubleQuotedPath = string.Format(@"""{0}""",path);

#11


1  

You can also include the double quotes into single quotes.

您还可以将双引号包含到单引号中。

string str = '"' + "How to add doublequotes" + '"';

#12


0  

Put a backslash (\) before the double quotes. That should work.

在双引号之前放一个反斜杠(\)。这应该工作。

#13


0  

In C# you can use:

在c#中,您可以使用:

 string1 = @"Your ""Text"" Here";
 string2 = "Your \"Text\" Here";

#14


0  

in C# if we use "\" means that will indicate following symbol is not c# inbuild symbol that will use by developer. so in string we need double quotes means we can put "\" symbol before double quotes. string s = "\"Hi\""

在c#中,如果我们使用“\”意味着后面的符号不是c# inbuild符号,开发人员将使用它。所以在字符串中我们需要双引号意味着我们可以把"\"符号放在双引号之前。字符串s = " \ "你好\”“

#15


0  

"<i class=\"fa fa-check-circle\"></i>" is used with ternary operator with Eval() data binding:

""与Eval()数据绑定的三元运算符一起使用:

Text = '<%# Eval("bqtStatus").ToString()=="Verified" ? 
       Convert.ToString("<i class=\"fa fa-check-circle\"></i>") : 
       Convert.ToString("<i class=\"fa fa-info-circle\"></i>"

#1


227  

You need to escape them by doubling them (verbatim string literal):

您需要通过加倍它们来转义它们(逐字字符串):

string str = @"""How to add doublequotes""";

Or with a normal string literal you escape them with a \:

或者用一个普通的字符串文字你用一个\:

string str = "\"How to add doublequotes\"";

#2


36  

So you are essentially asking how to store doublequotes within a string variable? Two solutions for that:

你问的是如何在字符串变量中存储双引号?两种解决方案:

var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";

In order to perhaps make it a bit more clear what happens:

为了更清楚地说明发生了什么:

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";

#3


11  

If I understand your question properly, maybe you can try this:

如果我能正确理解你的问题,也许你可以试试:

string title = string.Format("<div>\"{0}\"</div>", "some text");

#4


5  

Another Note:

另注:

    string path = @"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi"; 
    string hh = string.Format("\"{0}\"", path);
    Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");

The real value of hh as passed will be "H:\MOVIES\Battel SHIP\done-battleship-cd1.avi".

通过的hh的真正价值将是“H:\电影《巴氏战舰-战列舰-cd1.avi》”。

When needing double double literals use: @"H:\MOVIES\Battel SHIP\done-battleship-cd1.avi"; Instead of: @"H:\MOVIESBattel SHIP\done-battleship-cd1.avi"; Because the first literals is for the path name and then the second literals are for the double quotation marks

当需要双双字时,使用:@“H:\电影\Battel船\done- battles- cd1.avi”;而不是:@“H:\ \ done-battleship-cd1.avi MOVIESBattel船”;因为第一个常量是路径名,第二个常量是双引号

#5


5  

If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.

如果你需要经常这样做,并且希望代码更简洁,你可能需要一个扩展方法。

This is really obvious code, but still I think it can be useful to grab and make you save time.

这是非常明显的代码,但是我仍然认为它可以帮助您获取并节省时间。

  /// <summary>
    /// Put a string between double quotes.
    /// </summary>
    /// <param name="value">Value to be put between double quotes ex: foo</param>
    /// <returns>double quoted string ex: "foo"</returns>
    public static string AddDoubleQuotes(this string value)
    {
        return "\"" + value + "\"";
    }

Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.

然后可以在每个字符串上调用foo. adddoublequotes()或“foo”. adddoublequotes()。

Hope this help.

希望这个有帮助。

#6


2  

You could use &quot; instead of ". It will be displayed correctly by the browser.

您可以使用“而不是“。浏览器将正确地显示它。

#7


2  

You can use $.

你可以使用美元。

Interpolated Strings: Used to construct strings. An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations. This feature is available in C# 6 and later versions.

内插字符串:用于构造字符串。内插字符串看起来像一个包含内插表达式的模板字符串。内插字符串返回一个字符串,该字符串用其字符串表示替换其包含的内插表达式。这个特性可以在c# 6和后续版本中使用。

string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";

#8


1  

Use either

使用

&dquo;
<div>&dquo;"+ title +@"&dquo;</div>

or escape the double quote:

或者避免重复引用:

\"
<div>\""+ title +@"\"</div>

#9


1  

Using string interpolation with working example:

使用字符串插值与工作示例:

var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>";  //Note the order of the $@
Console.WriteLine (string1);

Output

输出

<div>"Title between quotes"</div>

#10


1  

string doubleQuotedPath = string.Format(@"""{0}""",path);

#11


1  

You can also include the double quotes into single quotes.

您还可以将双引号包含到单引号中。

string str = '"' + "How to add doublequotes" + '"';

#12


0  

Put a backslash (\) before the double quotes. That should work.

在双引号之前放一个反斜杠(\)。这应该工作。

#13


0  

In C# you can use:

在c#中,您可以使用:

 string1 = @"Your ""Text"" Here";
 string2 = "Your \"Text\" Here";

#14


0  

in C# if we use "\" means that will indicate following symbol is not c# inbuild symbol that will use by developer. so in string we need double quotes means we can put "\" symbol before double quotes. string s = "\"Hi\""

在c#中,如果我们使用“\”意味着后面的符号不是c# inbuild符号,开发人员将使用它。所以在字符串中我们需要双引号意味着我们可以把"\"符号放在双引号之前。字符串s = " \ "你好\”“

#15


0  

"<i class=\"fa fa-check-circle\"></i>" is used with ternary operator with Eval() data binding:

""与Eval()数据绑定的三元运算符一起使用:

Text = '<%# Eval("bqtStatus").ToString()=="Verified" ? 
       Convert.ToString("<i class=\"fa fa-check-circle\"></i>") : 
       Convert.ToString("<i class=\"fa fa-info-circle\"></i>"