【问题标题】:Output text file is not giving me underscore symbol输出文本文件没有给我下划线符号
【发布时间】:2015-01-03 15:47:54
【问题描述】:

我正在阅读一个文本文件并进行一些对齐更改,同时删除文本文件第 3 列中的空格并将其替换为下划线 (_) 符号。 但不幸的是......当我的输出生成时,我可以看到下划线符号无缘无故地出现。谁能帮帮我,为什么会这样??

代码sn-p:

 public void just_create_text()
    {
        //Here we are exporting header
        string[] strLines = System.IO.File.ReadAllLines(textBox1.Text);
        string CarouselName = enter.Text;
        int[] cols = new int[] { 15, 15, 25, 15, 15 };
        StringBuilder sb = new StringBuilder();
        string line = RemoveWhiteSpace(strLines[0]).Trim();
        string[] cells = line.Replace("\"", "").Split('\t');

        for (int c = 0; c < cells.Length; c++)
            sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c])); 
         // here i am replacing the space with underscore...

        sb.Append("Location".PadRight(15));
        sb.Append("\r\n");

        int tmpCarousel = 0;
        int carouselNumber = 0;
        Dictionary<string, int> namesForCarousels = new Dictionary<string, int>();
        for (int i = 0; i < textfile.Count; i++)
        {


            for (int c = 0; c < cells.Length; c++)
                sb.Append(textfile[i].Cells[c].PadRight(cols[c]));

            string name = textfile[i].Cells[1];

            if (namesForCarousels.TryGetValue(name, out tmpCarousel) == false)
            {
                carouselNumber++;
                namesForCarousels[name] = carouselNumber;
            }
            var strCorousel = lstMX.Find(x => x.MAX_PN.Equals(name)).Carousel;
            strCorousel = (String.IsNullOrEmpty(strCorousel)) ? CarouselName : strCorousel;
            sb.Append(String.Format("{0}:{1}", strCorousel, carouselNumber).PadRight(15));

            sb.Append("\r\n");
        }
        System.IO.File.WriteAllText(@"D:\output.TXT", sb.ToString());
    }

private string RemoveWhiteSpace(string str)
    {
        str = str.Replace("  ", " ");
        if (str.Contains("  "))
            str = RemoveWhiteSpace(str);
        return str;
    }

输入文本文件:

Designator  MAX PN  Footprint   Center-X(mm)    Center-Y(mm)

"AC1"   "100-0177"  "CAPC1608N" "7.239" "82.677"
"AC2"   "100-0177"  "CAPC1608N" "4.445" "85.471"
 "C1"   "100-0211"  "0805M - CAPACITOR" "14.745"    "45.72"
 "C2"   "100-0230"  "CAPC3225N" "83.388"    "58.42"
 "C3"   "100-0145"  "CAPC1608N" "101.543"   "73.025"
 "C10"  "100-0145"  "CAPC1608N" "109.163"   "73.025"

在输出文本文件中,如果我们采用 C1 的情况,我需要如下所示:

 C1  100-0211    0805M_-_CAPACITOR   14.745     45.72   Location:3
  //here only problem is i am not getting the underscore in 0805M - CAPACITOR.
   //i am getting as same.. like 0805M - CAPACITOR

【问题讨论】:

  • 请显示您的RemoveWhiteSpace() 方法的代码。
  • 我错了还是用下划线替换空格只发生在标题上而不是输出行上?
  • 我也想知道textfile是什么类型。我没有看到它被定义。

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


【解决方案1】:

您似乎只是在您正在生成的输出字符串的标题上调用String.Replace

您需要将它应用到代码的这一部分中的文本(在for 循环内):

for (int c = 0; c < cells.Length; c++)
    sb.Append(textfile[i].Cells[c].PadRight(cols[c]));

应该是

for (int c = 0; c < cells.Length; c++)
    sb.Append(textfile[i].Cells[c].Replace(" ","_").PadRight(cols[c]));

因为我看不到您的完整输出文件,所以我不确定这是您遇到的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 2021-09-27
    • 2018-04-29
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2013-08-22
    • 2014-08-22
    相关资源
    最近更新 更多