Convert string array of numbers to int array

Posted on: Thu Sep 11 15:34:43 -0700 2008. Updated on: Thu Sep 11 15:57:49 -0700 2008.
Category: DotNet

Lets say you have a comma delimited list of id's in a string object. You want to get these id's into a int[] array. Here is how to do it quick:

string[] idList = someIDs.Split(',');

int[] intIdList = Array.ConvertAll(idList, new Converter<string, int>(StringToInt32));

You will also need to define the follow static function

public static int StringToInt32(string number)
{
     return Convert.ToInt32(number);
}

Wasn't that easy? Note that you can convert other object types too, not simply string to int.

Comments:

Fri Sep 12 11:12:33 -0700 2008
That is clever... Know I coud use this here and there


---------------------------------------------------

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.