【问题标题】:Split an existing indexed array拆分现有的索引数组
【发布时间】:2019-10-07 10:55:06
【问题描述】:

我正在尝试读取一个由逗号和两行分隔的 .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);
}

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

【问题讨论】:

  • "...遇到问题..." - 此信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
  • 我所要求的基本上是关于如何使用 .split 方法来拆分现有数组的信息,以上是我目前在我的迷你项目前半部分的进展。
  • 在上面的代码中,你哪里有问题?什么问题?什么是预期的输出?你没有描述你的问题。你的代码中的 cmets 说它在做你想做的事,那么问题出在哪里?
  • 所以您想要splitarray?如果是这样,您可能想解释更多...
  • 是的,到目前为止,我已经成功地设置了数组以使第一行和第二行被索引,现在我需要使用。 .split 命令用于分隔数组中的值并将它们索引到它们自己的数组中,我相信如果我设法弄清楚如何使用 .split 方法会发生什么,对于第一次使用这个平台时的模糊解释感到抱歉。跨度>

标签: c# arrays split


【解决方案1】:

这是我的解决方案,我在 foreach 循环完成读取 txt 文件的工作之前调用了数组,感谢 Toby 的帮助

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);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2018-01-08
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多