c# 将 List<string> 转为 ["","","",""] 这样的格式
时间: 2023-12-11 19:33:21 浏览: 67
C#中List〈string〉和string[]数组之间的相互转换
以下是将List<string>转换为["","","",""]格式的C#代码示例:
```csharp
List<string> list = new List<string>() { "a", "b", "c", "d" };
string[] array = list.ToArray();
string result = "[" + string.Join(",", array.Select(x => "\"" + x + "\"")) + "]";
Console.WriteLine(result); // 输出:["a","b","c","d"]
```
该代码首先将List<string>转换为string[]数组,然后使用LINQ的Select方法将每个字符串用双引号括起来,并使用string.Join方法将它们连接起来,最后在开头和结尾添加方括号即可。
阅读全文