我搜索了一种方法来拆分字符串,我找到了一个。
现在我的问题是,我不能使用它所描述的方法。C#IEnumerable <string>和字符串[]
这是要告诉大家,我
不能隐式转换类型 'System.Collections.Generic.IEnumerable' 到 '字符串[]'。
所提供的方法是:
public static class EnumerableEx
{
public static IEnumerable<string> SplitBy(this string str, int chunkLength)
{
if (String.IsNullOrEmpty(str)) throw new ArgumentException();
if (chunkLength < 1) throw new ArgumentException();
for (int i = 0; i < str.Length; i += chunkLength)
{
if (chunkLength + i > str.Length)
chunkLength = str.Length - i;
yield return str.Substring(i, chunkLength);
}
}
}
如何,他说,这是用来:
string[] result = "bobjoecat".SplitBy(3); // [bob, joe, cat]
阵列不实现IEnumerable –
@AmitKumarGhosh可以了'字符串[]'分配给IEnumerable的''问题是相反的:自从.NET Framework 2.0以来,您无法将'IEnumerable '分配给'string []' –
@AmitKumarGhosh,[数组执行IEnumerable](https://ideone.com/d8rBt6)在[运行时](https://msdn.microsoft.com/en-us/library/system.array.aspx#remarksToggle)。但是,正如Dennis_E所提到的,这不是问题的根源。 –
soon