Click here to Skip to main content
13,406,671 members (51,511 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 6 Nov 2012

Manipulating a string array using a Lambda Expression

, 7 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This code will accept an input string and will store the first character of every array element in minimal lines of code.


Recently I came across a scenario where I was needed to store only the first character of an input string array. It is easy to do it using loops, however I tried to get the best benefits of lambda expressions which will help write minimal number of lines to achieve this.

Using the code

In this case, I have an input array string and I want to crop everything except the first character from each array element. I am using the Substring method inside a lambda expression to achieve this. The code is as follows:

string[] input1 = new string[] { "Bharti", "Mangesh", "Akash", "Bhavya", "Chetan" };
//OR even if input is comma seperated, multi spaced string 
//string[] input1 = new string[] { "Bharti, Mangesh,Akash, Bhavya, Chetan" }; 

string instr = string.Join(",", input1); 
string[] output1 = instr.Split(',').Select(a => { var t = 
   a.Trim().ToUpper().Substring(0, 1); return t; }).ToArray<string>();

Here the string array output1 will store output as shown below:


The above code has limitation that it works in fair cases and does not handle null values. besides we can replace the substring method with first method inside lambda expression which will do the same task.

//input string which contain null element 
string input1 = "Bharti, Mangesh,Akash,, Bhavya, Chetan";
string[] instr = input1.Split(new char[]{','},  StringSplitOptions.RemoveEmptyEntries);
IEnumerable<char> ie = instr.Where(item => item != null).Select(item => item.TrimStart().First()); 

Here the output enumerable can be converted to char array using ToArray.

We can achive this result using Loop also which goes like this:

string[] output1 = new string[instr.Length];
for (int i = 0, j = instr.Length; i < j; i++)
     string item = instr[i];
     if (!string.IsNullOrWhiteSpace(item))
          output1[i] = new String(item.TrimStart().Substring(0, 1).ToUpper().ToCharArray());


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


About the Author

Software Developer (Senior) Melstar Infotech Ltd.
India India
"Neeraj is very hardworking and talented guy. i worked with him for 3 years,he is helping in nature and will resolved any difficulty or coding problem with lot of enthusiasm with quality deliverable."

(Courtesy: LinkedIn endorsement posted by my ex-colleague Roshan Basekar)

- Thanks Roshan

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
zlanizer6-Nov-12 18:29
memberzlanizer6-Nov-12 18:29 
GeneralRe: My vote of 1 Pin
Neeraj_Maurya7-Nov-12 1:04
memberNeeraj_Maurya7-Nov-12 1:04 
General[My vote of 2] Post of poor quality Pin
Matej Hlatky6-Nov-12 11:14
memberMatej Hlatky6-Nov-12 11:14 
GeneralRe: [My vote of 2] Post of poor quality Pin
Neeraj_Maurya7-Nov-12 4:17
memberNeeraj_Maurya7-Nov-12 4:17 
General[My vote of 2] Clever, but fewer lines of code is not the best metric Pin
Matt T Heffron6-Nov-12 9:53
memberMatt T Heffron6-Nov-12 9:53 
GeneralRe: [My vote of 2] Clever, but fewer lines of code is not the best metric Pin
Neeraj_Maurya7-Nov-12 4:21
memberNeeraj_Maurya7-Nov-12 4:21 
GeneralMy vote of 5 Pin
Christian Amado6-Nov-12 9:15
memberChristian Amado6-Nov-12 9:15 
GeneralRe: My vote of 5 Pin
Neeraj_Maurya6-Nov-12 18:13
memberNeeraj_Maurya6-Nov-12 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180221.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by Neeraj_Maurya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid