Home » ASP.Net, C# Codes, Tech Corner

Convert a String in CSV format to INT Array in .NET

6 October 2009 No Comments Posted By:Vivek

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:

  1. XML serialization of Objects in .NET
  2. Populating ASP.NET TreeView Control from DataBase
  3. ConfigurationManager Class and the use of web.config file
  4. Creating your first WebService using .NET
  5. Edit,Update,Cancel In a GridView

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>