IEnumerable的&LT;焦炭&GT;字符串 [英] IEnumerable<char> to string
问题描述
我从来没有碰到这个偶然发现过,但我现在已经和很惊讶,我找不到一个非常简单的方法来转换一个的IEnumerable&LT;焦炭&GT;
来一个字符串
。
我能想到的最好的办法就是字符串str =新的字符串(myEnumerable.ToArray());
,但对我来说,好像这将创建一个新的的char []
,然后创建一个新的字符串
从,这似乎是昂贵的。
我会一直以为这是建立在.NET Framework中的某处常见功能。有没有一种简单的方法来做到这一点?
对于那些有兴趣,我想用这个的原因是使用LINQ过滤字符串:
字符串allowedString =新的字符串(inputString.Where(C =&GT; allowedChars.Contains(C))ToArray的());
您可以使用 String.Concat()
。
VAR allowedString = String.Concat(
inputString.Where(C =&GT; allowedChars.Contains(C))
);
买者:这种方法会有一些性能问题。看来, String.Concat
不字符的特例集合如此看来它执行,如果每个字符被转换为字符串然后再连接( <一个HREF =http://referencesource-beta.microsoft.com/mscorlib/a.html#630a858d9b6f4c5d>,它实际上确实 )。当然这给你一个内建的方式来完成这个任务,但它可以做的更好。
我不认为有框架内的任何实现,将特殊情况字符
所以你必须实现它。一个简单的循环附加字符的字符串生成器是很简单的创建。
下面是一些基准,我花了开发计算机上,它看起来是正确的。
百万迭代上300字符序列在32位版本编译:
ToArrayString:00:00:03.1695463
的concat:00:00:07.2518054
StringBuilderChars:00:00:03.1335455
StringBuilderStrings:00:00:06.4618266
静态只读的IEnumerable&LT;焦炭&GT; SEQ = Enumerable.Repeat('一个',300);静态字符串ToArrayString(IEnumerable的&LT;焦炭&GT;的CharSequence)
{
返回新的String(charSequence.ToArray());
}静态字符串连接(IEnumerable的&LT;焦炭&GT;的CharSequence)
{
返回String.Concat(CharSequence的);
}静态字符串StringBuilderChars(IEnumerable的&LT;焦炭&GT;的CharSequence)
{
VAR SB =新的StringBuilder();
的foreach(在CharSequence的变种C)
{
sb.Append(C);
}
返回sb.ToString();
}静态字符串StringBuilderStrings(IEnumerable的&LT;焦炭&GT;的CharSequence)
{
VAR SB =新的StringBuilder();
的foreach(在CharSequence的变种C)
{
sb.Append(c.ToString());
}
返回sb.ToString();
}
I've never stumbled across this before, but I have now and am surprised that I can't find a really easy way to convert an IEnumerable<char>
to a string
.
The best way I can think of is string str = new string(myEnumerable.ToArray());
, but, to me, it seems like this would create a new char[]
, and then create a new string
from that, which seems expensive.
I would've thought this would be common functionality built into the .NET framework somewhere. Is there a simpler way to do this?
For those interested, the reason I'd like to use this is to use LINQ to filter strings:
string allowedString = new string(inputString.Where(c => allowedChars.Contains(c)).ToArray());
You can use String.Concat()
.
var allowedString = String.Concat(
inputString.Where(c => allowedChars.Contains(c))
);
Caveat: This approach will have some performance implications. It seems that String.Concat
doesn't special case collections of characters so it seems that it performs as if every character was converted to a string then concatenated (and it actually does). Sure this gives you a builtin way to accomplish this task, but it could be done better.
I don't think there are any implementations within the framework that will special case char
so you'll have to implement it. A simple loop appending characters to a string builder is simple enough to create.
Here's some benchmarks I took on a dev machine and it looks about right.
1000000 iterations on a 300 character sequence on a 32-bit release build:
ToArrayString: 00:00:03.1695463 Concat: 00:00:07.2518054 StringBuilderChars: 00:00:03.1335455 StringBuilderStrings: 00:00:06.4618266
static readonly IEnumerable<char> seq = Enumerable.Repeat('a', 300);
static string ToArrayString(IEnumerable<char> charSequence)
{
return new String(charSequence.ToArray());
}
static string Concat(IEnumerable<char> charSequence)
{
return String.Concat(charSequence);
}
static string StringBuilderChars(IEnumerable<char> charSequence)
{
var sb = new StringBuilder();
foreach (var c in charSequence)
{
sb.Append(c);
}
return sb.ToString();
}
static string StringBuilderStrings(IEnumerable<char> charSequence)
{
var sb = new StringBuilder();
foreach (var c in charSequence)
{
sb.Append(c.ToString());
}
return sb.ToString();
}
这篇关于IEnumerable的&LT;焦炭&GT;字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!