用“@”符号连接字符串中的变量
Concatenate variables in string with "@" symbol
给出这个多行字符串:
string strProviderJSON = @"
{
""npi"":""1111111111"",
""name"":""DME Clearinghouse"",
""email"":""my@doc.como"",
""contactName"":""Sally Smith"",
""fax"":"""",
""address"":{
""country"":""United States"",
""street1"":""27787 Dequindre Apt 616"",
""street2"":"""",
""city"":""Madison Heights"",
""state"":""MI"",
""zipCode"":""32003""
},
""phone"":""(904) 739-0300"",
""contactPhone"":""(904) 739-0300""
}
";
如何在其中连接变量?我试过了,但一直收到错误消息:
string strTest = "1111111111";
string strProviderJSON = @"
{
""npi"":""" + strTest + """,
""name"":""DME Clearinghouse"",
""email"":""my@doc.como"",
""contactName"":""Sally Smith"",
""fax"":"""",
""address"":{
""country"":""United States"",
""street1"":""27787 Dequindre Apt 616"",
""street2"":"""",
""city"":""Madison Heights"",
""state"":""MI"",
""zipCode"":""32003""
},
""phone"":""(904) 739-0300"",
""contactPhone"":""(904) 739-0300""
}
";
将另一个 @
字符附加到下一个字符串文字的开头。
...
""npi"":""" + strTest + @"""
""name"": ""DME Clearinghouse"",
...
对于字符串连接,您应该使用 String.Format() 方法,因为 正常连接会在内存中创建多个字符串 所以 String.Format() 将帮助您轻松插入变量。
示例:
String s = "111111";
String finalString = String.Format(@""npi"":""{0}"",s);
这是针对单行的(您可以使用多行,但那样会不可读)。
现在对于多行你可以使用 StringBuilder class 它为我们提供了很多功能,比如 Append(), AppendFormat () 等所以使用它你可以有一个可读的代码
示例:
StringBuilder tempString = new StringBuilder();
tempString.Append("{\n");
tempString.AppendFormat(@"""npi"":""{0}"",\n", npiString);// npiString is a variable
tempString.AppendFormat(@"""name"":""{0}"",\n", nameString);// nameString is a variable
.....
// you should add variables like this
// at the end you can store final string by using following
String finalString = tempString.ToString();
注意:我没有在 String 中多次使用这些 '"' 所以不确定,但是附加变量应该由 StringBuilder.
完成
希望它能帮助您实现目标。
给出这个多行字符串:
string strProviderJSON = @"
{
""npi"":""1111111111"",
""name"":""DME Clearinghouse"",
""email"":""my@doc.como"",
""contactName"":""Sally Smith"",
""fax"":"""",
""address"":{
""country"":""United States"",
""street1"":""27787 Dequindre Apt 616"",
""street2"":"""",
""city"":""Madison Heights"",
""state"":""MI"",
""zipCode"":""32003""
},
""phone"":""(904) 739-0300"",
""contactPhone"":""(904) 739-0300""
}
";
如何在其中连接变量?我试过了,但一直收到错误消息:
string strTest = "1111111111";
string strProviderJSON = @"
{
""npi"":""" + strTest + """,
""name"":""DME Clearinghouse"",
""email"":""my@doc.como"",
""contactName"":""Sally Smith"",
""fax"":"""",
""address"":{
""country"":""United States"",
""street1"":""27787 Dequindre Apt 616"",
""street2"":"""",
""city"":""Madison Heights"",
""state"":""MI"",
""zipCode"":""32003""
},
""phone"":""(904) 739-0300"",
""contactPhone"":""(904) 739-0300""
}
";
将另一个 @
字符附加到下一个字符串文字的开头。
...
""npi"":""" + strTest + @"""
""name"": ""DME Clearinghouse"",
...
对于字符串连接,您应该使用 String.Format() 方法,因为 正常连接会在内存中创建多个字符串 所以 String.Format() 将帮助您轻松插入变量。
示例:
String s = "111111";
String finalString = String.Format(@""npi"":""{0}"",s);
这是针对单行的(您可以使用多行,但那样会不可读)。 现在对于多行你可以使用 StringBuilder class 它为我们提供了很多功能,比如 Append(), AppendFormat () 等所以使用它你可以有一个可读的代码
示例:
StringBuilder tempString = new StringBuilder();
tempString.Append("{\n");
tempString.AppendFormat(@"""npi"":""{0}"",\n", npiString);// npiString is a variable
tempString.AppendFormat(@"""name"":""{0}"",\n", nameString);// nameString is a variable
.....
// you should add variables like this
// at the end you can store final string by using following
String finalString = tempString.ToString();
注意:我没有在 String 中多次使用这些 '"' 所以不确定,但是附加变量应该由 StringBuilder.
完成希望它能帮助您实现目标。