The string.Split method is a great tool that can be used when manipulating strings. But, this method simply returns an array of strings when frequently I need a collection returned in the type that the strings actually represent. To fix this you simply have to loop through each item in the string array and convert it to its representative type. For example, if you are reading in a comma delimited file of temperatures, you can split on the commas and then loop through the returned array, parsing each element and adding it to a new list, as follows:
string temperatures = "67.2,92.1,78.2,100.3,89.2";
string[] tempArray = temperatures.Split(',');
List<float> temps = new List<float>();
foreach (string temp in tempArray)
temps.Add(float.Parse(temp));
While such a solution is common and quite acceptable, I wanted to make it simpler and reusable. To do such, I decided to implement the above logic in a generic extension method.
/// <summary>
/// Splits a string using the supplied separator and casts each element to the
/// indicated type.
/// </summary>
/// <typeparam name="T">The type of the List to return</typeparam>
/// <param name="s">The string on which the operation will be performed.</param>
/// <param name="separator">An array of strings that delimit the substrings in this string, an empty
/// array that contains no delimiters, or null.</param>
/// <returns>A List of type T containing the elements formed after splitting the string using
/// the given separators.</returns>
public static List<T> SplitToList<T>(this string s, params string[] separator)
{
return s.SplitToList<T>(StringSplitOptions.None, separator);
}
/// <summary>
/// Splits a string using the supplied separator and casts each element to the
/// indicated type.
/// </summary>
/// <typeparam name="T">The type of the List to return</typeparam>
/// <param name="s">The string on which the operation will be performed.</param>
/// <param name="options">Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
/// elements from the array returned, or System.StringSplitOptions.None to include
/// empty array elements in the array returned.</param>
/// <param name="separator">An array of strings that delimit the substrings in this string, an empty
/// array that contains no delimiters, or null.</param>
/// <returns>A List of type T containing the elements formed after splitting the string using
/// the given separators.</returns>
public static List<T> SplitToList<T>(this string s, StringSplitOptions options, params string[] separator)
{
// Split the string based on the supplied separators
string[] array = s.Split(separator, options);
List<T> values = new List<T>();
// Convert each element in the array to the indicated type
foreach (string element in array)
values.Add((T)Convert.ChangeType(element, typeof(T)));
return values;
}
Now our temperature example can be reduced to the following:
string temperatures = "67.2,92.1,78.2,100.3,89.2";
List<float> temps = temperatures.SplitToList<float>(",");
Below are some other examples of how this extension method could be used:
string s1 = "true-false-true-true-true-false";
List<bool> bools = s1.SplitToList<bool>("-");
string s2 = "a b c d e f g h i j";
List<char> chars = s2.SplitToList<char>(" ");
// Split on both commas and periods
string s3 = "1,3,4.5.7,1.4,6.7,8.2";
List<int> ints = s3.SplitToList<int>(",", ".");

November 27, 2010 at 11:09 am
[...] In order to alleviate this problem, I have created the following method below to correctly compare two version numbers. This method uses the SplitToList extension which takes a string, splits it based on a given string and then converts each element to the specified type. You can find a more detailed explanation and the code here. [...]
July 1, 2012 at 11:35 pm
Just to throw in my two cents, you can also do this in a one liner.
str.Split(separator).Select(s=> (T)Convert.ChangeType(s, typeof(T)));