仅供参考:不足之处请大家多多指教
string.Join 一些常用方法
测试类:
public class Test
{
public string Name { get; set; }
public string Age { get; set; }
}
测试代码:
数组或者是List<string> 类型的数据使用方式如下:
//实例化数组
string[] Tstring = {"1","2","3"};
//实例化泛型
List<string> ListString = new List<string>()
{
"1","2","3","4"
};
//拼接数组类型字符串
string Cstring = string.Join(",",Tstring);
//拼接泛型类字符串
string TestString = string.Join(",", ListString);
//输出数组类型字符串 结果为:1,2,3
Console.WriteLine(Cstring);
//输出范类型字符串 结果为:1,2,3,4
Console.WriteLine(TestString);
//下面泛型类,想拼接其中字段的字符串需要如下操作如:
//实例化泛型类
List<Test> List = new List<Test>()
{
new Test() {Name="x",Age="1"},
new Test() {Name="xx",Age="2"},
new Test() {Name="xxx",Age="3"},
new Test() {Name="xxxx",Age="4"},
};
//拼接泛型类字符串
string TestList = string.Join(",",from a in List select a.Age);
//输出结果为 1,2,3,4
Console.WriteLine(TestList);