Hi - I have a problem where I have a list of strings, and i need to split up the components of each string in the list and create a new array with the reordered data in the right format.

For example: I have

(line 1) PDIA20031.MV PDIA45141.MV TIA20102.MV TICA20033.MV TICA20033.OP
(line 2) 01.62 00.83 100.76 30.58 --.--
(line 3) 01.62 00.83 100.77 30.58 --.--
(line 4) 01.60 00.83 100.78 30.57 --.--
(line 5) --.-- 00.83 100.79 30.57 --.--
(line 6) --.-- 00.82 100.80 30.57 --.--
(line 7) 01.63 00.83 100.81 30.58 --.--
etc.

I need to split the string up into an array with:

Data(x,y)
Data(0,0) = PDIA20031.MV
Data(0,1) = 01.62
Data(0,2) = 01.62
etc
Data(1,0) = PDIA45141.MV
Data(1,1) = 00.83
Data(1,2) = 00.83
etc
Data(2,0) = PDIA45141.MV
Data(2,1) = 100.76
Data(2,2) = 100.77
etc

I have been trying to think how to get round this, and have started using the split function for each line. However, I am confused as how to get the split looping through the list for each line (string) and getting it to write each component of the string into the required place in the array.

Thanks for your help so far, this forum is great for (slowly) getting me up to speed with C#. Any help would be greatly appreciated. :)

Si01

Recommended Answers

All 5 Replies

List<string> list = new List<string>();
        list.Add("PDIA20031.MV PDIA45141.MV TIA20102.MV TICA20033.MV TICA20033.OP");
        list.Add("01.62 00.83 100.76 30.58 11.22");
        list.Add("01.62 00.83 100.76 30.58 11.22");

        foreach(string s in list)
        {
            foreach (string ele in s.Split(' '))
                Console.WriteLine(ele);
        }

Thanks for your reply. I have as few questions:

The list of data changes - will this code allow me to add all the strings in the list regardless of content. I was thinking of having some way to loop through the list, seperating the strings with the split function, and then writing them to an array until there is a blank line (end of list) at which point the function would end.

Is this possible? Thanks

Si01

Use List<string,string>.

This question is a duplicate of another poster. Are you and 666kennedy in the same class? :)

using System;
using System.Collections.Generic;

namespace daniweb
{
  class MultiArray
  {
    private static string[] GetTestData()
    {
      List<string> result = new List<string>();
      result.Add("return1.MV TE244654.MV b43tgwg.sp");
      result.Add("01.62 00.83 100.76");
      result.Add("01.62 00.83 100.77");
      result.Add("01.60 00.83 100.78");
      result.Add("--.-- 00.83 100.79");
      result.Add("--.-- 00.82 100.80");
      result.Add("01.63 00.83 100.81");
      result.Add("01.63 00.83 100.82");
      result.Add("--.-- 00.83 100.83");
      result.Add("--.-- 00.83 100.84");
      result.Add("01.62 00.83 100.85");
      result.Add("01.59 00.83 100.86");
      result.Add("--.-- 00.83 100.88");
      result.Add("--.-- 00.83 100.89");
      result.Add("01.61 00.84 100.90");
      result.Add("01.60 00.84 100.91");
      return result.ToArray();
    }
    internal static void DoWork()
    {
      string[] testData = GetTestData();
      int xLen = testData[0].Split(new char[] { ' ' }, StringSplitOptions.None).Length;
      int yLen = testData.Length;
      string[,] result = new string[xLen, yLen];

      for (int iY = 0; iY < testData.Length; iY++)
      {
        string[] fields = testData[iY].Split(new char[] { ' ' }, StringSplitOptions.None);
        if (fields.Length != xLen)
          throw new Exception("You have a varying number of columns....");

        for (int iX = 0; iX < fields.Length; iX++)
        {
          result[iX, iY] = fields[iX];
        }
      }
      System.Diagnostics.Debugger.Break();
    }
  }
}

This looks like the same question that 666kennedy asked:
http://www.daniweb.com/forums/thread209016.html

Hey - yes we are doing the same project, but are at different stages. I did read through that thread first, but I have no prior programming experience, so do not understand what is really going on! :S

If anyone can explain to me how the items of the list are put into the correct place in the arrray, I would really appreciate it!

Thanks for everyones help do far!

:)
si01

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.