Given the string below:
string str = "1,2,3";
Will this be the best extension to convert it to an int
array?
static class StringExtensions
{
public static int[] ToIntArray(this string s)
{
return ToIntArray(s, ',');
}
public static int[] ToIntArray(this string s, char separator)
{
string[] ar = s.Split(separator);
List<int> ints = new List<int>();
foreach (var item in ar)
{
int v;
if (int.TryParse(item, out v))
ints.Add(v);
}
return ints.ToArray();
}
}