如何在C#中更改字符串格式

时间:2019-06-02 08:59:27

标签: c# asp.net asp.net-mvc asp.net-mvc-4

如何更改字符串格式。 MATCH (city1:City {name: 'Foo'}) OPTIONAL MATCH (st:Street {streetName: 'streetName1'})-[:BELONGS_TO]->(city1) FOREACH(x IN CASE WHEN st IS NULL THEN [1] END | CREATE (st2:Street {streetName: 'streetName1'})-[:BELONGS_TO]->(city1)) 2,4,6,9

[2],[4],[6],[9]

结果我得到 int[] terms = termsList.ToArray(); var result = string.Join(",", terms); ,但我想得到2,4,6,9格式

2 个答案:

答案 0 :(得分:3)

string result = String.Join(",", terms.ToList().ConvertAll(i => "["+i.ToString()+"]"));

优雅的方法是

string result = String.Join(",", terms.Select(x => "[" + x + "]"));

我喜欢这个,

string result = string.Join(",", Array.ConvertAll(terms, s => $"[{s}]"));

POC:.net Fiddle

答案 1 :(得分:3)

您可以使用LINQ和字符串插值:

var terms = new []{ 2, 4, 6, 9 };
var str = string.Join(",", terms.Select(x => $"[{x}]"));

结果

  

[2],[4],[6],[9]