拆分现有的索引数组

时间:2019-05-21 05:31:06

标签: c# arrays split

我正在尝试读取以逗号和两行分隔的.txt文件,目前我已对其进行设置,以便它可以读取txt文件,但是在使用taxArray的.split方法时遇到了麻烦,我需要使用.split,以便我可以计算不同的值。

string[] taxArray = new string[2];
int count = 0;

try
{
    if(File.Exists(filePath))
    {
        //read the lines from text file add each line to its own array index.
        foreach (string line in File.ReadLines(filePath, Encoding.UTF8))
        {
            taxArray[count] = line;
            txtDisplay.Text += taxArray[count] + "\r\n";
            count++;
        }
    }
    else
    {
        MessageBox.Show("This file cannot be found");
    }
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

这当前完全按照我的需要输出。

1 个答案:

答案 0 :(得分:0)

这是我的解决方案,在foreach循环读取txt文件之前,我正在调用数组,感谢Toby <3

的帮助
string[] taxArray = new string[2];
            int count = 0;
            // string incomeBracket = taxArray[0];

            //string[] incomeBracketArray = incomeBracket.Split(',');

            try
            {
                if(File.Exists(filePath))
                {

                    //read the lines from text file add each line to its own array index.
                    foreach (string line in File.ReadLines(filePath, Encoding.UTF8))
                    {
                        taxArray[count] = line;
                        txtDisplay.Text += taxArray[count] + "\r\n";
                        count++;
                    }
                    string[] incomeBracket = taxArray[1].Split(',');
                    txtDisplay.Text = incomeBracket[0];

                else
                {
                    MessageBox.Show("This file cannot be found");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }