Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Manipulating a string array using a Lambda Expression

, 7 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Manipulating a string array using a Lambda Expression"

Introduction

This is an alternative to tip Manipulating a string array using a Lambda Expression.

The idea is to take the first character of each string and return the results as an array. 

Using the code

For the test, I expanded the string array to contain also an empty string and a null value to verify that the code runs as expected also in these situations.

To extract the first letters, a query like the following can be written:

string[] inputarray = new string[] { "Bharti", "Mangesh", "", null, "Akash", "Bhavya", "Chetan" };
char[] outputarray = (from item in inputarray
                      where item != null
                      select item.FirstOrDefault<char>()).ToArray(); 

So the above code loops through all the elements in the input array. In order to prevent null exceptions, elements containing a null value are skipped using the where clause.

For each non-null element the first character is extracted using FirstOrDefault method. Because the input array may contain empty strings, it's important to also include the default value in the select clause. 

So the result is:  

Element index Char value Char 
66 'B' 
77 'M' 
'\0' 
65 'A' 
66 'B' 
67 'C' 
 

The same query can also be written like the  the following (the actual code line is split for readability): 

char[] outputarray = inputarray.Where(item => item != null)
                               .Select(item => item.FirstOrDefault<char>())
                               .ToArray();  

These are just few examples how the character can be extracted. 

One additional example could be, if the third character should always be returned, the statement could look like this (again, the actual code line is split for readability): 

char[] outputarray = inputarray.Where(item => item != null)
                               .Select(item => item.Skip<char>(2).FirstOrDefault<char>())
                               .ToArray(); 

So before taking the first (or the default) character, two characters are skipped with the Skip method. With the test data, the result would be:  

Element index Char value Char 
97 'a' 
1  110 'n' 
'\0' 
97 'a' 
97 'a' 
101 'e' 

References

References for the methods used:

History  

  • November 7, 2012: Alternative created. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mika Wendelius
Architect
Finland Finland
I've been a programmer since mid 80's using languages like assembler, C/C++, PL/I (mainframe environment), pascal, VB (I know, I know, no comments please) and C# and utilizing different techniques and tools.
 
However I'm specialized in databases and database modeling. Mostly I have used products like Oracle (from version 6), SQL Server (from version 4.2), DB2 and Solid Server (nowadays an IBM product).
 
For the past 10+ years my main concerns have been dealing with different business processes and how to create software to implement and improve them. At my spare time (what ever that actually means) I'm also teaching and consulting on different areas of database management, development and database oriented software design.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by Mika Wendelius
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid