2017-03-16 80 views
-1

我有一个字符串数组,我想要解析通过使每个组的4个对象的字符串。这可以通过for-loop来完成吗?从字符串数组C中获取元素组#

唯一标识一个组的是每个组将包含4个字符串。

数组看起来像这样,其中最高值“NumberOfItemsInArray”将是一个数字,告诉有多少个项目组遵循。项目的数量是动态的。

NumberOfItemsInArray 
Item1-Name 
Item1-Price 
Item1-DiscountRate 
Item1-Category 
Item2-Name 
Item2-Price 
Item2-DiscountRate 
Item2-Category 

有什么办法可以干净地做到这一点?

+0

请**阐明您的具体问题**或添加其他详细信息以确切地突出显示您需要的内容。正如目前所写,很难确切地说出你在问什么。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask)页面以获得澄清此问题的帮助。 –

+0

基本上我想循环遍历一个看起来像所描述的数组,并将每个组的4个字符串分配给一个新的独立数组。你如何用for循环做到这一点? –

+0

你尝试了什么? –

回答

2

假设你有YourObject一些构造函数的4串(A,B,C,d),它应该是相当简单的,因为你的输入字符串数组,MYARRAY和输出List<YourObject> YourObjectList:

int iItems = System.Convert.ToInt32(MyArray[0]); 
for (int i = 0; i < iItems; i+=4) 
    YourObjectList.Add(new YourObject(MyArray[i+1], MyArray[i+2], MyArray[i+3], MyArray[i+4])); 

这应该是有关的权利 - 没有经过编译运行,虽然
约翰 -

+0

这工作得很好,谢谢! –

0

确定根据您的数组的长度的四组数。然后只需遍历原始数组,并填充四个组的数组。

string[] itemsArray = { "8", "a", "b", "c", "d", "e", "f", "g", "h" }; 

    int nGroups = (itemsArray.Length - 1)/4; 
    string[][] groups = new string[nGroups][]; 

    for (int i = 0; i < nGroups; i++) { 
     print("- new group of four -"); 
     groups[i] = new string[4]; 
     for (int j = 0; j < 4; j++) { 
      groups[i][j] = itemsArray[i * 4 + j + 1]; 
      print(groups[i][j]); 
     } 
    } 

输出:

- new group of four - 
a 
b 
c 
d 
- new group of four - 
e 
f 
g 
h 
0

NumberOfItemsInArray看起来很没用,因为你有Array.Lenght,我认为你的阵列是不是阵列,我认为这是字典,如果是的话那么看看这个:

using System; 
using System.Collections.Generic; 

namespace Program { 
    class _Main { 
     // Initialize dictionary with values 
     static Dictionary<string, string> dictionaryWithStrings = new Dictionary<string, string> { 
      { "NumberOfItemsInArray", "2" }, 
      { "Item1-Name", "Some name" }, 
      { "Item1-Price", "0.5$" }, 
      { "Item1-DiscountRate", "?%" }, 
      { "Item1-Category", "Some category" }, 
      { "Item2-Name", "Other name" }, 
      { "Item2-Price", "4$" }, 
      { "Item2-DiscountRate", "24%" }, 
      { "Item2-Category", "Other category" } 
     }; 
     // Create *dynamic* list with Item's 
     static List<Item> objects = new List<Item>(); 
     static void Main() {    
      // "- 1" removes the NumberOfItemsInArray from count 
      // "/ 4" Since you have always 4 strings 
      for (int i = 1; i <= (dictionaryWithStrings.Count - 1)/4; i++) { 
       objects.Add(new Item { 
        Name = dictionaryWithStrings["Item"+i+"-Name"], 
        Price = dictionaryWithStrings["Item"+i+"-Price"], 
        DiscountRate = dictionaryWithStrings["Item"+i+"-DiscountRate"], 
        Category = dictionaryWithStrings["Item"+i+"-Category"] 
       }); 
      } 
      Console.WriteLine("objects contains [" + objects.Count + "] items, values:"); 
      foreach (Item item in objects) { 
       Console.WriteLine("Item id: [" + objects.IndexOf(item) + 
        "], Name: [" + item.Name + 
        "], Price: [" + item.Price + 
        "], DiscountRate: [" + item.DiscountRate + 
        "], Category: [" +item.Category + "]" 
       ); 
      } 
     } 
     struct Item { 
      public string Name; 
      public string Price; 
      public string DiscountRate; 
      public string Category; 
     } 
    } 
}