【问题标题】:Output name varies on different line输出名称在不同的行上有所不同
【发布时间】:2014-12-30 05:43:57
【问题描述】:

我正在编写一个程序来根据我的第二个 cloumn 名称在文本文件中写入一些名称,如您在此处看到的 https://imageshack.com/i/eyW14lH6j。我的输出视图显示在链接中。 所以我所做的是,它根据 MAX_PN 列名逐行读取第二列的文本及其生成名称。

例如:在上面的输出视图中,我需要相同的字符串(100-0145 出现,我需要显示 Carousel:4) 我想如果第二列名称相似,我需要相同的名称出现在 Location 列中。但是我的代码发生了什么, 如果一个或两个三行之后出现相同的名称,则它不会读取。我该如何纠正这一点。请帮帮我。

我的代码sn-p:

            int[] cols = new int[] { 15, 15, 25, 15, 15 };
            string[] strLines = System.IO.File.ReadAllLines(textBox1.Text);

            StringBuilder sb = new StringBuilder();
            string line = string.Empty;
            string LastComment = string.Empty;
            string CarouselName = "Carousel";
            int iCarousel = 0;

            for (int i = 0; i < strLines.Length; i++)
            {
                line = RemoveWhiteSpace(strLines[i]).Trim();
                string[] cells = line.Replace("\"", "").Split('\t');
                for (int c = 0; c < cells.Length; c++)
                    sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c]));

                if (cells.Length > 1)
                {
                    if (cells[1] != LastComment & i > 0)
                    {
                        iCarousel++;
                        if (iCarousel > 45)
                            iCarousel = 1;
                        LastComment = cells[1];
                    }

                    if (i == 0)
                        sb.Append("Location".PadRight(15));
                    else
                        sb.Append(String.Format("{0}:{1}", CarouselName, iCarousel).PadRight(15));
                }
                sb.Append("\r\n");
            }

            System.IO.File.WriteAllText(textBox1.Text, sb.ToString());

【问题讨论】:

  • 因此您想从文本文件中读取 MAX_PN,然后为每个 MAX_PN 编号分配一个唯一的 Carousel:x 其中 x 是一个数字?
  • @Mihai 是的,非常......

标签: c# wpf visual-studio-2010 visual-studio replace


【解决方案1】:

尝试将所有名称保存在字典中,其中 int 是轮播的编号。 然后,您可以检查与您的姓名匹配的轮播。 看看这个样本:

Dictionary<string,int> namesForCarousels = new Dictionary<string,int>();

...

if (cells.Length > 1)
{
    var name = cells[1];
    int carouselNumber;
    if (namesForCarousels.TryGetValue(name, out carouselNumber) == false)
        {
            carouselNumber = iCarousel++;
            namesForCarousels[name] = carouselNumber;
        }
    if (i == 0)
    sb.Append("Location".PadRight(15));
    else
    sb.Append(String.Format("{0}:{1}", CarouselName, carouselNumber).PadRight(15));
}

【讨论】:

  • 显示错误..Error The name 'namesForCarousels' does not exist in the current context
  • 请你看看我的问题stackoverflow.com/questions/26723495/…>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2021-05-14
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多