我有两个字符串/文本文件:“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];
}
}
我看到的一件事就是你有2个if(orderID [2] == id [i])'行,我假设你想要说'if(orderID [1] == id [i ])'而不是,也可能是您在同一代码块(发布代码中最后一个for循环)中使用'name [1]'作为'name [2]'的一个名称。 – Quantic