Convert a String in CSV format to INT Array in .NET
There may be many instances in which we may have to supply a string in CSV format to be converted into an integer array.For example,if we have to invoke a web method in a web service in which the parameters to be passed is an integer array.There is no way that we can pass an Integer array to the web method,else you would have to have a client application to test the method.Well,this was the instance in which i had the need to pass these integers in CSV format as a string and get the result.
Here is the code to convert a string in CSV format ot an integer array.
public int[] StringToArray(string input)
{
string[] stringList = input.Split(",".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
int[] ids = new int[stringList.Length];
for (int i = 0; i < stringList.Length; i++)
{
ids[i] = (int)Convert.ChangeType(stringList[i], typeof(int));
}
return ids;
}
Here instead of the “,” delimiter you can make use of the delimiter used in your format
Related posts:








Leave your response!