Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Separating numbers joined by "," into an array

Separating numbers joined by "," into an array

Scheduled Pinned Locked Moved C#
helpquestiondata-structuresxmltutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:

    for (int i = 0; i <= xmlnode.Count-1; i++)
    {
    int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
    }

    But, inside the pranthesis has an error which says:

    cannot convert from 'string[] to string'

    an example of the XML content is:

    1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872

    How can I solve this problem?

    L D OriginalGriffO 3 Replies Last reply
    0
    • A Alex Dunlop

      I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:

      for (int i = 0; i <= xmlnode.Count-1; i++)
      {
      int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
      }

      But, inside the pranthesis has an error which says:

      cannot convert from 'string[] to string'

      an example of the XML content is:

      1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872

      How can I solve this problem?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The String.Split method returns an array of strings. And you cannot pass an array to int.Parse. You first need to capture the array and then convert the numbers individually. Also, you should always use TryParse to convert strings to numbers, in order to avoid exceptions.

      1 Reply Last reply
      0
      • A Alex Dunlop

        I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:

        for (int i = 0; i <= xmlnode.Count-1; i++)
        {
        int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
        }

        But, inside the pranthesis has an error which says:

        cannot convert from 'string[] to string'

        an example of the XML content is:

        1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872

        How can I solve this problem?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        You put the .Split INSIDE the parameter of the Parse call. Split will return a string[] (string array), not a string. You have to do the split outside of the Parse, then parse each value separately and create your int[] from those values. Parse will only work on individual strings, not arrays of them.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        A 1 Reply Last reply
        0
        • D Dave Kreskowiak

          You put the .Split INSIDE the parameter of the Parse call. Split will return a string[] (string array), not a string. You have to do the split outside of the Parse, then parse each value separately and create your int[] from those values. Parse will only work on individual strings, not arrays of them.

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          A Offline
          A Offline
          Alex Dunlop
          wrote on last edited by
          #4

          Thanks, I solved it

          1 Reply Last reply
          0
          • A Alex Dunlop

            I have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:

            for (int i = 0; i <= xmlnode.Count-1; i++)
            {
            int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
            }

            But, inside the pranthesis has an error which says:

            cannot convert from 'string[] to string'

            an example of the XML content is:

            1,-65536,-655362,-16711872,-167118723,-16711872,-167118724,-16711872,-167118725,-16711872,-167118726,-16711872,-167118727,-16711872,-16711872

            How can I solve this problem?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            A Linq based solution might be one line of code:

            using System;
            using System.Linq;

            public class Program
            {
            public static void Main()
            {
            Console.WriteLine("Hello World");
            int[] ints = "1,2,3,4,666".Split(',').Select(s => int.Parse(s)).ToArray();
            foreach (int i in ints)
            {
            Console.WriteLine(i);
            }
            }
            }

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups