197

次のような変数があります。

string title = string.empty;

私が必要としているのは、渡された文字列が何であれ、二重引用符で囲まれたdiv内のコンテンツを表示する必要があるということです。だから私は次のようなものを書いています:

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

しかし、ここに二重引用符を追加するにはどうすればよいですか? 次のように表示されます。

"How to add double quotes"
4

19 に答える 19

380

それらを2倍にしてエスケープする必要があります(逐語的な文字列リテラル):

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

または、通常の文字列リテラルでは、次のようにエスケープします\

string str = "\"How to add doublequotes\"";
于 2010-10-11T12:01:21.597 に答える
56

つまり、本質的に、文字列変数内に二重引用符を格納する方法を尋ねていますか? そのための2つのソリューション:

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

おそらく何が起こるかをもう少し明確にするために:

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";
于 2010-10-11T12:01:28.133 に答える
20

これを頻繁に行う必要があり、コードをよりクリーンにしたい場合は、このための拡張メソッドが必要になる場合があります。

これは非常に明白なコードですが、それでも取得して時間を節約できると便利だと思います。

  /// <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 + "\"";
    }

次に、好きな文字列ごとに foo.AddDoubleQuotes() または "foo".AddDoubleQuotes() を呼び出すことができます。

于 2013-05-29T07:54:31.563 に答える
15

私があなたの質問を正しく理解していれば、これを試すことができます:

string title = string.Format("<div>\"{0}\"</div>", "some text");
于 2010-10-11T12:03:28.817 に答える
7

二重引用符を一重引用符に含めることもできます。

string str = '"' + "How to add doublequotes" + '"';
于 2017-10-14T07:10:29.500 に答える
3

実際の例で文字列補間を使用する:

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

出力

<div>"Title between quotes"</div>
于 2016-11-30T00:20:25.137 に答える
2

&quot;の代わりに使用できます"。ブラウザで正しく表示されます。

于 2010-10-11T12:02:26.710 に答える
1

C# では、次を使用できます。

 string1 = @"Your ""Text"" Here";
 string2 = "Your \"Text\" Here";
于 2016-05-06T12:59:56.590 に答える
1

どちらかを使用

&dquo;
<div>&dquo;"+ タイトル +@"&dquo;</div>

または二重引用符をエスケープします。

\"
<div>\""+ タイトル +@"\"</div>
于 2010-10-11T12:04:04.353 に答える
0

二重引用符の前にバックスラッシュ (\) を付けます。それはうまくいくはずです。

于 2010-10-11T12:01:47.180 に答える
0

HTMLで二重引用符を追加したい場合

echo "<p>Congratulations, &#8220;" . $variable . "&#8221;!</p>";

出力

Congratulations, "Mr John"!
于 2018-09-25T10:10:52.253 に答える
0

各行を「-」で開始して、箇条書きリストを作成します。

于 2020-11-05T08:55:14.657 に答える