Monday, December 19, 2011

Remove all leading and lagging empty spaces from items in an array

I was looking for a way to efficiently remove all leading and lagging empty spaces from items in an array in as few lines of code as possible.  In this particular instance, the goal was to read items into an array from the web.config file and then use the Trim() method to remove all leading and lagging empty spaces from each item.  Here is the code that will do this activity in just two lines for a particular, comma-delimited AppSetting node called ChargeCodesToUse in my web.config file:

string[] chargeCodesToUse = ConfigurationManager.AppSettings["ChargeCodesToUse"].ToString().Split(',');
Array.ForEach(chargeCodesToUse, chargeCodes => chargeCodesToUse[Array.IndexOf(chargeCodesToUse, chargeCodes)] = chargeCodes.Trim());

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.