从IList<string>或IEnumerable<string>创建逗号分隔的字符串值列表的最干净的方法是什么?
string . join(…)操作在字符串[]上,因此当IList<string>或IEnumerable<string>等类型不能轻松转换为字符串数组时,处理起来很麻烦。
从IList<string>或IEnumerable<string>创建逗号分隔的字符串值列表的最干净的方法是什么?
string . join(…)操作在字符串[]上,因此当IList<string>或IEnumerable<string>等类型不能轻松转换为字符串数组时,处理起来很麻烦。
当前回答
要从IList<string>或IEnumerable<string>创建一个逗号分隔的列表,除了使用string. join()外,还可以使用StringBuilder。AppendJoin方法:
new StringBuilder().AppendJoin(", ", itemList).ToString();
or
$"{new StringBuilder().AppendJoin(", ", itemList)}";
其他回答
希望这是最简单的方法
string Commaseplist;
string[] itemList = { "Test1", "Test2", "Test3" };
Commaseplist = string.join(",",itemList);
Console.WriteLine(Commaseplist); //Outputs Test1,Test2,Test3
我能看到的最简单的方法是使用LINQ聚合方法:
string commaSeparatedList = input.Aggregate((a, x) => a + ", " + x)
通过性能比较,获胜者是“循环它,附加它,并做后退”。 实际上“可枚举的和手动移动下一步”是一样的好(考虑stddev)。
BenchmarkDotNet=v0.10.5, OS=Windows 10.0.14393
Processor=Intel Core i5-2500K CPU 3.30GHz (Sandy Bridge), ProcessorCount=4
Frequency=3233539 Hz, Resolution=309.2587 ns, Timer=TSC
[Host] : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
Clr : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
Core : .NET Core 4.6.25009.03, 64bit RyuJIT
Method | Job | Runtime | Mean | Error | StdDev | Min | Max | Median | Rank | Gen 0 | Allocated |
---------------------- |----- |-------- |---------:|----------:|----------:|---------:|---------:|---------:|-----:|-------:|----------:|
StringJoin | Clr | Clr | 28.24 us | 0.4381 us | 0.3659 us | 27.68 us | 29.10 us | 28.21 us | 8 | 4.9969 | 16.3 kB |
SeparatorSubstitution | Clr | Clr | 17.90 us | 0.2900 us | 0.2712 us | 17.55 us | 18.37 us | 17.80 us | 6 | 4.9296 | 16.27 kB |
SeparatorStepBack | Clr | Clr | 16.81 us | 0.1289 us | 0.1206 us | 16.64 us | 17.05 us | 16.81 us | 2 | 4.9459 | 16.27 kB |
Enumerable | Clr | Clr | 17.27 us | 0.0736 us | 0.0615 us | 17.17 us | 17.36 us | 17.29 us | 4 | 4.9377 | 16.27 kB |
StringJoin | Core | Core | 27.51 us | 0.5340 us | 0.4995 us | 26.80 us | 28.25 us | 27.51 us | 7 | 5.0296 | 16.26 kB |
SeparatorSubstitution | Core | Core | 17.37 us | 0.1664 us | 0.1557 us | 17.15 us | 17.68 us | 17.39 us | 5 | 4.9622 | 16.22 kB |
SeparatorStepBack | Core | Core | 15.65 us | 0.1545 us | 0.1290 us | 15.45 us | 15.82 us | 15.66 us | 1 | 4.9622 | 16.22 kB |
Enumerable | Core | Core | 17.00 us | 0.0905 us | 0.0654 us | 16.93 us | 17.12 us | 16.98 us | 3 | 4.9622 | 16.22 kB |
代码:
public class BenchmarkStringUnion
{
List<string> testData = new List<string>();
public BenchmarkStringUnion()
{
for(int i=0;i<1000;i++)
{
testData.Add(i.ToString());
}
}
[Benchmark]
public string StringJoin()
{
var text = string.Join<string>(",", testData);
return text;
}
[Benchmark]
public string SeparatorSubstitution()
{
var sb = new StringBuilder();
var separator = String.Empty;
foreach (var value in testData)
{
sb.Append(separator).Append(value);
separator = ",";
}
return sb.ToString();
}
[Benchmark]
public string SeparatorStepBack()
{
var sb = new StringBuilder();
foreach (var item in testData)
sb.Append(item).Append(',');
if (sb.Length>=1)
sb.Length--;
return sb.ToString();
}
[Benchmark]
public string Enumerable()
{
var sb = new StringBuilder();
var e = testData.GetEnumerator();
bool moveNext = e.MoveNext();
while (moveNext)
{
sb.Append(e.Current);
moveNext = e.MoveNext();
if (moveNext)
sb.Append(",");
}
return sb.ToString();
}
}
使用https://github.com/dotnet/BenchmarkDotNet
我在寻找一个好的c#方法来连接字符串时,就像使用MySql方法CONCAT_WS()一样,进行了这个讨论。此方法与string.Join()方法不同,它不会在字符串为NULL或空时添加分隔符。
CONCAT_WS (', ', tbl Lastname, tbl Firstname)。
如果名字为空,将只返回Lastname,同时
管柱。(", ", strLastname, strFirstname)
将返回strLastname + ", "在同样的情况下。
想要第一个行为,我写了以下方法:
public static string JoinStringsIfNotNullOrEmpty(string strSeparator, string strA, string strB, string strC = "")
{
return JoinStringsIfNotNullOrEmpty(strSeparator, new[] {strA, strB, strC});
}
public static string JoinStringsIfNotNullOrEmpty(string strSeparator, string[] arrayStrings)
{
if (strSeparator == null)
strSeparator = "";
if (arrayStrings == null)
return "";
string strRetVal = arrayStrings.Where(str => !string.IsNullOrEmpty(str)).Aggregate("", (current, str) => current + (str + strSeparator));
int trimEndStartIndex = strRetVal.Length - strSeparator.Length;
if (trimEndStartIndex>0)
strRetVal = strRetVal.Remove(trimEndStartIndex);
return strRetVal;
}
您可以使用ToArray将IList转换为数组,然后运行字符串。在阵列上执行Join命令。
Dim strs As New List(Of String)
Dim arr As Array
arr = strs.ToArray