GSharper

Wednesday 25 April 2007

Convert integer array to string array

Today I needed to write some code to convert an integer array to a string array. Somebody told me to iterate over the integer array and fill the string array.

I said to myself NO WAY. .NET has to have some build-in functionality to do this stuff. And it does. Array.Convert Generic Method is the name.

This is the code:

private void TestMethod(int[] intArray)
{
string[] stringArray =
Array.ConvertAll
<int,string>
(intArray,
new Converter<int,string>
(ConvertIntToString));

string result = string.Join(",", stringArray);
}

private string ConvertIntToString(int intParameter)
{
return intParameter.ToString();
}


Greetz,                                                                              G

15 Comments:

  • Never looked at the overloads, but I have programmed this in a for loop multiple times. Great, tnx!

    By Anonymous Anonymous, At 14 May 2007 at 16:32  

  • That's just awesome. I was looking for exactly that. Thanks!

    By Anonymous Anonymous, At 31 August 2007 at 15:20  

  • this is the same but more compact.
    depends on your preference.

    string[] stringArray = Array.ConvertAll<int,string>(testIds, delegate(int intParameter) {return intParameter.ToString();});

    By Anonymous Anonymous, At 20 February 2008 at 18:55  

  • Or, you can use Convert.ToString() as your delegate like so:

    string[] stringArray = Array.ConvertAll<int,string>(intArray,new Converter<int,string>(Convert.ToString));

    By Anonymous Anonymous, At 11 April 2008 at 19:46  

  • Thanks! (To all, even the commenters helped refine it!)

    By Blogger MrEs, At 11 June 2008 at 03:51  

  • Or maybe you take:
    Array.ConvertAll<int, string>(INTARRAY, intParameter => intParamter.ToString())

    :)

    By Anonymous Anonymous, At 17 November 2008 at 12:53  

  • or even better:

    var STRINGARRAY = INTARRAY.Select(s => s.ToString());

    By Anonymous Anonymous, At 17 November 2008 at 12:58  

  • hi, i need to know how to convert a string to an array of integer in C# .net

    By Anonymous Anonymous, At 13 January 2009 at 15:43  

  • Convert.ToInt32()

    By Blogger IGD, At 18 January 2009 at 21:30  

  • if someone changes localization in control panel, does this affect the final result.

    By Anonymous Anonymous, At 9 March 2009 at 12:53  

  • Long live linq !!

    string[] stringArray = intArray.Select(i => i.ToString()).ToArray():

    By Blogger Unknown, At 22 April 2010 at 08:56  

  • return Array.ConvertAll(this.PdfFolders.Split(','), s => Int32.Parse(s));

    By Anonymous Anonymous, At 18 May 2010 at 14:46  

  • Hey excellent blog! Does running a blog such as this require a massive amount work?
    I have absolutely no knowledge of coding however I had been hoping to
    start my own blog soon. Anyways, if you have any recommendations or tips for new blog owners please
    share. I know this is off subject nevertheless I simply had to ask.
    Cheers!

    Feel free to surf to my page: best registry Cleaner
    Also see my webpage :: windows xp Registry cleaner

    By Anonymous Anonymous, At 19 February 2013 at 15:13  

  • Nice blog rіght here! Addіtіonаlly your web sitе a lоt up vегy fast!
    What host are you the use of? Can I am getting your affiliаte hypегlink on уouг host?
    Ι want my site loaded up as fast аs yours lol

    Feel free to vіsit my homepаgе hcg diet drops

    By Anonymous Anonymous, At 22 February 2013 at 11:49  

  • Hi there, I would like to subscribe for this weblog to get most recent updates, therefore where can i do it please help.



    Also visit my weblog :: graduate certificate online

    By Anonymous Anonymous, At 9 March 2013 at 10:29  

Post a Comment

Subscribe to Post Comments [Atom]



<< Home