【发布时间】: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