Jak uniknąć nawiasów klamrowych (nawiasy klamrowe) w łańcuchu formatującym in.NET
Jak można zabezpieczyć nawiasy przy użyciu string.Format
.
Na przykład:
String val = "1,2,3"
String.Format(" foo {{0}}", val);
Ten przykład nie wyrzuca wyjątku, ale wypisuje łańcuch foo {0}
.
Czy istnieje sposób na ucieczkę z nawiasów?
10 answers
Aby wyjść foo {1, 2, 3}
musisz zrobić coś w stylu:
string t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t);
Do wyjścia a {
używasz {{
, A do wyjścia a }
używasz }}
.
Lub teraz, Można również użyć interpolacji C# string w ten sposób (funkcja dostępna w C# 6.0)
Nawiasy Specjalne: Interpolacja Łańcucha Znaków $(""). jest to nowa funkcja w C # 6.0
var inVal = "1, 2, 3";
var outVal = $" foo {{{inVal}}}";
//Output will be: foo {1, 2, 3}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-28 14:55:38
Yes to output {
in string.Format
you have to escape it like this {{
Więc to
String val = "1,2,3";
String.Format(" foo {{{0}}}", val);
Wyświetli "foo {1,2,3}"
.
Ale musisz wiedzieć o błędzie projektowym w C# , który polega na tym, że idąc zgodnie z powyższą logiką założysz, że poniższy kod wydrukuje {24.00}
int i = 24;
string str = String.Format("{{{0:N}}}", i); //gives '{N}' instead of {24.00}
Ale to drukuje {N}. Dzieje się tak, ponieważ sposób, w jaki C# parsuje sekwencje specjalne i formatuje znaki. Aby uzyskać pożądaną wartość w powyższym przypadku, musisz użyć tego zamiast.
String.Format("{0}{1:N}{2}", "{", i, "}") //evaluates to {24.00}
Odniesienie Artykuły String.Format gottach i String formatowanie FAQ
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-09-21 08:29:44
Już prawie! Sekwencja ucieczki dla klamry to {{
lub }}
, więc dla Twojego przykładu użyjesz:
string t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-13 15:16:47
Możesz użyć podwójnych nawiasów otwartych i podwójnych nawiasów zamykających, które pokażą tylko jeden nawias na twojej stronie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-26 20:44:25
Unikanie nawiasów klamrowych i użycie interpolacji string stanowi interesujące wyzwanie. Musisz użyć poczwórnych nawiasówaby uciec interpolacji string parsowanie i string.format
parsowanie.
Znaki specjalne nawiasów: Interpolacja String $ ( "" ) i String.Format
string localVar = "dynamic";
string templateString = $@"<h2>{0}</h2><div>this is my {localVar} template using a {{{{custom tag}}}}</div>";
string result = string.Format(templateString, "String Interpolation");
// OUTPUT: <h2>String Interpolation</h2><div>this is my dynamic template using a {custom tag}</div>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-20 19:30:43
Przyszedł tutaj w poszukiwaniu sposobu budowania ciągów json ad-hoc (bez serializacji klasy / obiektu) w C#. Innymi słowy, jak uniknąć nawiasów klamrowych i cudzysłowów podczas używania interpolowanych łańcuchów w C#i " dosłownych literałów łańcuchowych" (podwójne cytowane Ciągi z prefiksem'@'), jak np...
var json = $@"{{""name"":""{name}""}}";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-06 20:01:35
[TestMethod]
public void BraceEscapingTest()
{
var result = String.Format("Foo {{0}}", "1,2,3"); //"1,2,3" is not parsed
Assert.AreEqual("Foo {0}", result);
result = String.Format("Foo {{{0}}}", "1,2,3");
Assert.AreEqual("Foo {1,2,3}", result);
result = String.Format("Foo {0} {{bar}}", "1,2,3");
Assert.AreEqual("Foo 1,2,3 {bar}", result);
result = String.Format("{{{0:N}}}", 24); //24 is not parsed, see @Guru Kara answer
Assert.AreEqual("{N}", result);
result = String.Format("{0}{1:N}{2}", "{", 24, "}");
Assert.AreEqual("{24.00}", result);
result = String.Format("{{{0}}}", 24.ToString("N"));
Assert.AreEqual("{24.00}", result);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-06 16:48:54
Lub możesz użyć interpolacji w c# string w ten sposób (funkcja dostępna w C# 6.0)
var value = "1, 2, 3";
var output = $" foo {{{value}}}";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-03 16:54:01
Nawiasy Specjalne: Interpolacja Łańcucha Znaków $("") :
Teraz możesz również użyć interpolacji C# string w ten sposób (funkcja dostępna w C# 6.0)
var inVal= "1, 2, 3";
var outVal= $" foo {{{inVal}}}";
//Output will be: foo {1, 2, 3}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-10-11 14:01:35
Mój cel:
Musiałem przypisać wartość "{CR}{LF}"
do zmiennej string
delimiter
.
Kod c#:
string delimiter= "{{CR}}{{LF}}";
Uwaga: Aby uniknąć znaków specjalnych normalnie trzeba użyć . Aby otworzyć klamry {, użyj jednego dodatkowego typu {{. Do zamknięcia klamry} użyj jednej dodatkowej }}.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-11 16:10:02