如何在使用中转义括号string.Format
.例如:
String val = "1,2,3"
String.Format(" foo {{0}}", val);
Run Code Online (Sandbox Code Playgroud)
此示例不会抛出异常,但会输出字符串 foo {0}
有没有办法摆脱括号?
可能重复:
在String.Format中转义大括号'{'
c#有一个String.Format方法,允许您格式化字符串,但是使用标记插入params {0}
{1}
我试图创建一个简单的json字符串,需要大括号在字符串中,因此它打破了格式化程序
String.Format("{ foo:'{0}', bar:'{1}' }", foo, bar);
Run Code Online (Sandbox Code Playgroud)
在括号之前添加一个逃脱没有帮助
抛出一个异常,说我的字符串格式不正确,有人知道怎么解决这个问题吗?
我正在使用 Bodybuilder 和邮件发送服务发送 HTML 电子邮件模板。我用这个链接学习。
https://www.c-sharpcorner.com/article/send-email-using-templates-in-asp-net-core-applications/
无论如何,我使用外部服务 ( https://topol.io/ )创建了我的电子邮件模板。
当我尝试将生成的 HTML 文件与第一个链接中引用的 String.FOrmat 命令一起使用时,它给了我错误。我终于发现是这部分代码导致 String.Format 失败。
这是我通常与其他 HTML 模板一起使用的 c# 代码:
using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
{
builder.HtmlBody = SourceReader.ReadToEnd();
}
string messagebody = string.Format(builder.HtmlBody,
//Not important
);
Run Code Online (Sandbox Code Playgroud)
现在,HTML 中的这种样式代码会导致 String.Format 崩溃(或 bodybuilder,我不确定究竟是哪个问题存在,它在 string.format 处失败)。
<style type="text/css">
#outlook a {
padding: 0;
}
.ReadMsgBody {
width: 100%;
}
.ExternalClass {
width: 100%;
}
.ExternalClass * {
line-height: 100%;
}
body {
margin: 0;
padding: 0;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%; …
Run Code Online (Sandbox Code Playgroud) 如何在C#中格式化字符串,其中模式有括号?当我运行以下声明时......
String.Format("Foo { Bar={0} }", this.Bar);
Run Code Online (Sandbox Code Playgroud)
...我收到运行时异常:
System.FormatException:输入字符串的格式不正确.
我是否应该逃避括号?如何?
我想忽略{字符串中的字符是否可能?
感谢帮助
String.Format("function(s, e) { {0}.PerformCallback(MyObject.GetSelectedItem().value); }", getClientName);
Run Code Online (Sandbox Code Playgroud) 我想插入一个字符串的中间,但我不能使用String.Format
,因为该字符串包含{}
大括号.
这是我到目前为止所尝试的:
string code = "(function(){var myvariable = $"{variableToBeInterpolated}"});"
Run Code Online (Sandbox Code Playgroud)
返回:)预期; 预期
编辑:我尝试了下面的代码片段,插值现在正在工作,但代码不会在浏览器中执行.
"(function(){var myvariable=" + $"{myvariable c#}" + ")});"
Run Code Online (Sandbox Code Playgroud) 我有这个方法,我已经简化了重现问题,任何人都可以解释为什么抛出系统格式异常?
我已经尝试将@添加到格式字符串的开头,以防万一它是转义字符的问题,但它没有帮助.
private void doThing(StringBuilder builder, string inPrimaryKey) {
// At this point the builder.ToString() results in " <div class="v-group width-100 shadowed OrderPanel"
// and inPrimaryKey is "OrderId"
// Throws System.FormatException with the detail "Input string was not in a correct format."
builder.AppendFormat(@" @if (Model.{0} > 0) { <text>StateNonEditable</text> } else { <text>StateEditable</text> }", inPrimaryKey);
}
Run Code Online (Sandbox Code Playgroud)
有点背景,我们使用这段代码生成一个cshtml页面供web应用程序使用,所以stringbuilder最初包含一些html,然后我们在格式部分添加了一些C#MVC Razor.
这让我摸不着头脑.
我有一个函数返回一个包含自动完成控件的类似json数据的字符串,但无法String.Format
工作:
private string GetUsers(string crit)
{
string result = "";
using (DataTable dt = dl.GetRecordSet("select ob_id, ob_desc from objects where ac_id = @acid and ob_desc like @crit and ot_id = 29 and ob_deleted = 0 order by ob_desc", new[] { MyApplicationSession.AccountId, "%" + crit + "%" }))
{
result = "[ ";
foreach (DataRow dr in dt.Rows)
{
result += string.Format("{ \"id\": \"{0}\", \"label\": \"{1}\", \"value\": \"{1}\" },", dr["ob_id"].ToString(), dr["ob_desc"].ToString());
}
result = result.Substring(0, result.Length - 1); …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码行收到此错误,
int numberStored = 9;
record.VALUE = string.Format("{\"FIELDS\":[{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}]}", numberStored.ToString(), 0);
Run Code Online (Sandbox Code Playgroud)
如果我替换{0}并将9直接放入,这样可以正常工作,但显然我不希望它像这样硬编码.我能看到的这个问题以前的答案似乎没有帮助我.
c# ×9
.net ×2
string ×2
.net-core ×1
c#-4.0 ×1
curly-braces ×1
formatting ×1
html ×1
parsing ×1
razor ×1