How to convert string collection to integer or custom types

In this post I will discuss implementation of data conversion task that is performed very commonly in application. For example lot of time we put some data in configuration files. When we store data in configuration file, it is presented to us as a string or collection of strings. There are some simple methods available that can do simple casting of this string data to types like int, bool etc. But what do you do when the data needs more custom conversion. Some of the questions that I get from users are:

  • How to convert string collection to int list
  • How to convert array of string values to integers
  • How to convert collection of string values to custom type

Here is an example from one of my applications.

<add key="ItemIds" value="1,34,56,76,9" />
<add key="ItemColors" value="blue, yellow, red" />

These are two entries in configuration file. Fist one is list of IDs and second is list of colors. As you can see that list of IDs is going to be collection of integers and second one I need to convert to color codes.

There is Cast extension method available. If you try to use that, you will get null value back. For example following code does not work because what we have is a comma separated list of string values. So a cast to INT will fail.

var values = Config.Default.ItemId;
IEnumerable<int> itemIds = values.Cast<int>();

So how can we convert this comma separated list of values to collection of integer or custom types. There is a very handy extension method ConvertAll available on of all collection types that will allow you to perform this conversion. Lets look at signature of this method available on Array class.

public static TOutput[] ConvertAll<TInput, TOutput>(
    TInput[] array,
	Converter<TInput, TOutput> converter

This generic method allows us to specify type of input and output. And the heart of this method is Converter method. It allows you to specify the method that will be used to convert data from input type to output type. In some simple cases you can use the methods already available in the framework. And in cases where you need to do custom conversion, then you can implement your own method and provide that as converter.

Using Converter Implemented in .Net Framework

Here is an example that uses Convert.Int32 method as converter to convert array of string values into list of integers.

 var values = Config.Default.ItemId.Split(',');
 IEnumerable itemIds = Array.ConvertAll<.string, int>(values, Convert.ToInt32);

Using custom converter

Here is an example of custom converter method that takes string values of colors from my configuration file and then convert them into codes.

var colorValues = Config.Default.ItemColors.Split(',');
IEnumerable itemColors = Array.ConvertAll<string, int>(colorValues, ColorTextToCode);

public static int ColorTextToCode(string value)
  switch (value.Trim().ToUpper())
    case "BLUE":
      return 1;
    case "RED":
      return 2;
    case "YELLOW":
      return 3;
      return 4;

You can extend this to perform conversions to any custom type. This conversion is not limited to primitive types or simple types. You can use write converters to create objects of complex types.

comments powered by Disqus

Blog Tags