我有三个字符串列表,目的是将这些列表用分隔符组合成一个单独的字符串。
List<string> list1=new List<string>{"A","B","C"};
List<string> list2=new List<string>{"=","<", ">"};
List<string> list3=new List<string>{"1","2","3"};
最终输出结果如下:
A=1 AND B<2 AND C>3
有没有简单的方法生成最终字符串?我用了for循环,但这似乎很丑陋。我知道C#中的字符串有Join
方法,可以使用分隔符将数组组合起来。如何使用分隔符组合多个数组?
以下是我的代码:
StringBuilder str = new StringBuilder();
for(int i=0; i< list1.count; i++)
{
str.AppendFormat("{0}{1}{2} AND ", list1[i], list2[i], list3[i]);
}
str.Length = str.Length -5;
string final = str.ToString();
List<Comparison>
,并用属性来表示Operator
、Variable
和Value
呢?没有必要通过索引连接多个集合,这通常是需要重构的代码坏味道。这个类可以重写ToString
方法来返回你所需要的内容。 - Tim Schmelterstr.Length = str.Length - 5;
不起作用str.Length
是只读的。 - fubo