C#定义多行字符串的方式
在定义的前面加上@符号:
string aa = @"asdfsdfsdfsdsfsdfsdfsdfsdfsdfssafasfsadfsdfasfsfsdfsd ";
在C#中拼接字符串有几种方法
1. 利用 JsonConvert.SerializeObject方法 (Nuget获取Newtonsoft.Json Package)
string uid = "22";
var abcObject = new
{
AccessKey = 11,
CustomerNo = uid,
mc = "33",
qd = "44",
mr = "55",
insertDate = DateTime.Now
};
string serJson = JsonConvert.SerializeObject(abcObject);
2. 利用StringBuilder
StringBuilder str = new StringBuilder();
str.Append("{");
str.Append("AccessKey:\"" + 11 + "\",");
str.Append("mc:\"" + 22 + "\",");
str.Append("qd:\"" + 33 + "\"");
str.Append("}");
string serJson = str.ToString();
上面这样写,key/value键值对的key没有引号,容易出问题,修改如下:
StringBuilder str = new StringBuilder();
str.Append("{");
str.Append("\”AccessKey\":\"" + 11 + "\",");
str.Append("\"mc\":\"" + 22 + "\",");
str.Append("\"qd\":\"" + 33 + "\"");
str.Append("}");
string serJson = str.ToString();
3. 直接拼接字符串
string serJon = "{AccessKey:\"" + 11 + "\",mc:\"" + 22 + "\"}";
4. 利用StringFormat
string mc = "22";
string id = "11";
string serJson = string.Format("[{{ AccessKey:\"{0}\",mc:\"{1}\"}},{{ AccessKey:\"{2}\",mc:\"{3}\"}}]", id, mc, "33", "44");