2016-07-15 113 views
0

我有兩個字符串/文本文件:「1.dll」和「1a.dll」 - 1.dll包含「訂單ID」和「cartID」(用回車符「/ n」分隔) - 1a.dll是數據庫witdh「id」和「名稱」(用回車'/ n'分隔)C#中的字符串數組拆分

我將字符串拆分爲字符串數組。然後我用兩個字符串分隔每個數組字符串。一個是偶數位置,另一個是奇數位置。 分割這兩個文件後,我有4個數組字符串,我正在向4個ListBoxes顯示。 - 來自1.dll的2個數組正在顯示,因爲它們應該是 - 來自1a.dll的2個數組缺少一些值。 Here is the screenshot with problem

//Load and split "1.dll" > create 2 array strings. orderID=odd # position and cartID=even # position 
     string a = File.ReadAllText(@"order/1.dll"); 
     string[] aa = a.Split('\n'); 
     aa = aa.Select(s => (s ?? "").Trim()).ToArray(); 
     string[] orderID = new string[aa.Length]; 
     string[] cartID = new string[aa.Length]; 

     int Dial1 = 0; 
     int Dial2 = 0; 
     for (int i = 0; i < aa.Length; i++) 
     { 
      if (i % 2 == 0) 
      { 
       orderID[Dial1] = aa[i]; 
       Dial1++; 
      } 
      else 
      { 
       cartID[Dial2] = aa[i]; 
       Dial2++; 
      } 
     } 
     for (int j = 0; j < aa.Length/2; j++) 
     { 
      AddToCartList.Items.Add(cartID[j]); 
      OrderIDList.Items.Add(orderID[j]); 
     } 
//Load and split "1a.dll" > create 2 array strings. id=odd # position and game=even # position 

     string b = File.ReadAllText(@"order/1a.dll"); 
     string[] bb = b.Split('\n'); 
     bb = bb.Select(s => (s ?? "").Trim()).ToArray(); 
     string[] id = new string[bb.Length/2]; 
     id = id.Select(s => (s ?? "").Trim()).ToArray(); 
     string[] name = new string[bb.Length/2]; 
     name = name.Select(s => (s ?? "").Trim()).ToArray(); 
     string combindedString = string.Join("\n", bb.ToArray()); 
     MessageBox.Show(combindedString); 

     int Dial3 = 0; 
     int Dial4 = 0; 
     for (int i = 0; i < bb.Length/2; i++) 
     { 
      if (i % 2 == 0) 
      { 
       id[Dial3] = bb[i]; 
       Dial3++; 
      } 
      else 
      { 
       name[Dial4] = bb[i]; 
       Dial4++; 
      } 
     } 
     for (int j = 0; j < bb.Length/2; j++) 
     { 
      IDlist.Items.Add(id[j]); 
      nameList.Items.Add(name[j]); 
     } 




     for (int i = 0; i < id.Length; i++) 
     { 
      if (orderID[0] == id[i]) 
      { 
       textBox1.Text = name[0]; 
      } 
      if (orderID[2] == id[i]) 
      { 
       textBox2.Text = name[1]; 
      } 
      if (orderID[2] == id[i]) 
      { 
       textBox3.Text = name[1]; 
      } 
     } 
+0

我看到的一件事就是你有2個if(orderID [2] == id [i])'行,我假設你想要說'if(orderID [1] == id [i ])'而不是,也可能是您在同一代碼塊(發佈代碼中最後一個for循環)中使用'name [1]'作爲'name [2]'的一個名稱。 – Quantic

回答

1

在第二循環運行循環的BB陣列

for (int i = 0; i < bb.Length/2; i++) 

的內容的一半,這應該是

for (int i = 0; i < bb.Length; i++) 

但是從這段代碼可以使用被改變了很多開通用List<T>而不是創建許多臨時數組,

例如第一環路可以寫成

// ReadAllLines already returns your text file splitted at newlines 
string[] aa = File.ReadAllLines(@"order/1.dll"); 

// With lists you don't need to create a fixed size array in advance... 
List<string> orders = new List<string>(); 
List<string> carts = new List<string>(); 

// Your array could be iterated two items at times 
// Of course here a check for even number of items should be 
// added here.... 
for (int i = 0; i < aa.Length; i += 2) 
{ 
    orders.Add(aa[i]); 
    carts.Add(aa[i+1]); 
} 
// The collections have the possibility to add a range of items 
// without you writing a loop 
AddToCartList.Items.AddRange(carts.ToArray()); 
OrderIDList.Items.AddRange(orders.ToArray()); 
+0

完美的作品! 我會用1a.dll來做同樣的事情。 在這種情況下你有一個建議也許如何與列表做到這一點: '爲(INT I = 0;我 qretsar

+0

這部分對我來說不是很清楚。你有三個文本框,但我不明白設置Text屬性的規則。然而,列表可以用作數組,因爲它們是數組,因此代碼不需要僅僅因爲有列表而不是數組而改變。 – Steve

0

錯是這裏:

int Dial3 = 0; 
int Dial4 = 0; 
for (int i = 0; i < bb.Length/2; i++) 

長度應該是不bb.Length/2;

+0

我只想刪除問題。這不是對未來其他人有價值的東西。我不是主持人。這只是一個意見。 – Paparazzi