Posts Tagged MSDN
C# Short Takes – 2 – String from IEnumerable
Posted by aussiecraig in CSharp, LINQ, Programming on February 22, 2012
Introduction
I am posting this quick tip in response to a question, which seems to on some people’s minds who are reading my blog. The search terms ‘create string from ienumerable’ appear as hits on my blog statistics quite frequently. The way I create a string from an IEnumerable is in the Example Code section below.
The Method
The method I use to create a string from an IEnumerableis to use the string constructor that accepts a char[] (see the MSDN documentation: String Constructor (Char()) ). To get the IEnumerable into a char[] I use the Linq Extension Method ToArray() (see the MSDN documentation: Enumerable.ToArray(Of TSource) Method).
These Linq method call and string constructor results in the code in the ‘Example Code’ section.
The Example Code
The following shows a number of ways to create an IEnumerableobject. These IEnumerableobjects are then converted into char[] object. The char [] objects are then used in the string constructor.
private void String_Create_From_IEnumerable_char( ) { string TestString = "The quick red fox jumped over the lazy brown cow"; IEnumerable<char> EnumerableChar= TestString.AsEnumerable(); string Result1 = new string(EnumerableChar.ToArray( )); char[] TestCharArray = new char[] { 'T', 'h', 'e', ' ', 't', 'e', 's', 't', ',', 'v', 'a', 'l', 'u', 'e', '.' }; string Result2 = new string(TestCharArray); IEnumerable<char> EnumerableChar1 = TestCharArray.AsEnumerable( ); string Result3 = new string(EnumerableChar1.ToArray()); IEnumerable<char> EnumerableChar2 = LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable('A') .UnionAll(LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable('B')) .UnionAll(LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable('C')) .UnionAll(LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable('D')) .UnionAll(LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable('E')); string Result4 = new string(EnumerableChar2.ToArray( )); Func<char, IEnumerable<char>> LocalToIEnumerable1 = LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable<char>; IEnumerable<char> EnumerableChar3 = LocalToIEnumerable1('Z') .UnionAll(LocalToIEnumerable1('Y')).UnionAll(LocalToIEnumerable1('X')).UnionAll(LocalToIEnumerable1('W')) .UnionAll(LocalToIEnumerable1('V')).UnionAll(LocalToIEnumerable1('U')).UnionAll(LocalToIEnumerable1('T')); string Result5 = new string(EnumerableChar3.ToArray( )); Func<char, char, IEnumerable<char>> LocalToIEnumerable2 = LINQ_Extension_Methods.LINQ_Extension_Methods.ToIEnumerable<char>; IEnumerable<char> EnumerableChar4 = LocalToIEnumerable2('A', ' ') .UnionAll(LocalToIEnumerable2('B', ' ')).UnionAll(LocalToIEnumerable2('C', ' ')) .UnionAll(LocalToIEnumerable2('D', ' ')).UnionAll(LocalToIEnumerable2('E', ' ')) .UnionAll(LocalToIEnumerable2('F', ' ')).UnionAll(LocalToIEnumerable2('G', ' ')) .UnionAll(LocalToIEnumerable2('H', ' ')).UnionAll(LocalToIEnumerable2('I', ' ')); string Result6 = new string(EnumerableChar4.ToArray( )); return; }
Methods Referenced That Are Not In The.Net Framework
Method | Blog Post That Describes The Method |
ToIEnumerable | LINQ Extension Method to Generate n-way Cartesian Product |
UnionAll | LINQ Short Takes – Number 4 –Make Union into a UnionAll |
Conclusion
I trust that those readers who have been looking for a solution to this transformation find this blog post helpful answers the question you had.
Related articles
- Dumping a formatted IEnumerable to Output (craigwatson1962.wordpress.com)
- LINQ Short Takes – Number 2 – Using Method Syntax to Create a Cartesian Product (craigwatson1962.wordpress.com)
- LINQ Short Takes – Number 3 – LINQ over Multiple Dimension Arrays and Lists (craigwatson1962.wordpress.com)
- LINQ Extension Method To Dump any IEnumerable (craigwatson1962.wordpress.com)
- C# Short Takes – 1 – XML Comments syntax for the cref attribute to a Generic Type Method (craigwatson1962.wordpress.com)