C# 将数组、集合、可枚举类型转换成逗号分隔的字符串(String.Join)
作者:C/S框架网|www.cscode.ne  发布日期:2021-05-20 09:57:33
  C# 将数组、集合、可枚举类型转换成逗号分隔的字符串(String.Join)


C# Code:


public const int MAX_KEYWORDS = 20;

string searchInput="123,3456,347,234,5612,345,234,52,345,2as,fg,wert,w,ert,w,ert,24,56,347,";

//分解关键字,去掉空的关键字
string[] items = searchInput.Split(new char[] { char.Parse(",") }, StringSplitOptions.RemoveEmptyEntries);

//去重复关键词
items = items.Distinct().ToArray();

if (items.Length > MAX_KEYWORDS)
{
  
string[] copy = new string[MAX_KEYWORDS];
  Array.Copy(items,
0, copy, 0, MAX_KEYWORDS);//复制数组
  
  
//C# 将数组、集合、可枚举类型转换成逗号分隔的字符串(String.Join)
  
return String.Join(",", copy);
}
else
{
  
return String.Join(",", items);
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761



上一篇 下一篇