格式化字符串

时间:2012-08-18 03:15:20

标签: c# string

如何将此字符串列表转换为用引号括起来的逗号分隔值,而不包含任何转义字符?

{"apple", "berry", "cherry"} => well, ""apple", "berry", "cherry""

3 个答案:

答案 0 :(得分:1)

如果我理解正确的话,

"\"" + String.Join("\", \"", new string[]{"apple","berry","cherry"}) + "\"";

或者,

String.Format("\"{0}\"", String.Join("\", \"", new string[] {"apple","berry","cherry"}));

详细了解System.String.Join(...)

答案 1 :(得分:0)

希望这能完成这项工作

var ar = new []{ "apple", "berry", "cherry" };
var separator = "\",\"";
var enclosingTag = "\"";
Console.WriteLine ( enclosingTag + String.Join(separator, ar) + enclosingTag );

答案 2 :(得分:0)

如果您使用的是C#:

using System;
string[] arr = new string[] { "apple", "berry", "cherry" };
string sep = "\",\"";
string enclosure = "\"";
string result = enclosure + String.Join(sep, arr) + enclosure;