我正在写一个程序,根据我的第二个cloumn名称在文本文件中写一些名字,正如你在这里看到的https://imageshack.com/i/eyW14lH6j。我的输出视图显示在链接中。 所以我所做的是,它根据 MAX_PN列名逐行读取第二列及其生成名称。
对于例如:在上面的输出视图中,我需要一个相同的字符串(100-0145来我需要Carousel:4显示) 我希望如果第二列名称相似,我需要在位置列中显示相同的名称。但是我的代码发生了什么, 如果在一行或两行之后出现相同的名称则不会读取。我怎么能纠正这个。请帮帮我。
我的代码段:
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());
答案 0 :(得分:1)
尝试在Dictionary中保存所有名称,其中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));
}