繁体   English   中英

C#中的字符串数组拆分

[英]String array splitting in C#

我有两个字符串/文本文件:“ 1.dll”和“ 1a.dll”-1.dll包含“订单ID”和“ cartID”(用输入'/ n'分隔)-1a.dll是数据库witdh“ id ”和“名称”(以'/ n'分隔)

我将字符串拆分为字符串数组。 然后,我将每个数组字符串分成两个字符串。 一个带有偶数位置,另一个带有奇数位置。 拆分两个文件后,我将显示4个数组字符串,并将其显示到4个ListBoxes。 -来自1.dll的2个数组按应有的方式显示-来自1a.dll的2个数组缺少某些值。 这是有问题的屏幕截图

//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];
            }
        }

在第二个循环中,运行循环以获取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());

错误在这里:

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

长度应该是bb.Length不带/ 2;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM