Search code examples
c#visual-studio-2010visual-studioreplacestringreader

Output text file is not giving me underscore symbol


I am reading a text file and making some alignment changes, also removing the space in 3rd column of text file and replacing it with underscore (_) symbol. But unfortunately.. when my output generated i can see the underscore symbol is not coming for no reason. Can ANYONE help me please, why its happening like that??

code snippet:

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

Input text file:

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"

In output text file i need like below if we take the case for 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

Solution

  • It appears you are only calling String.Replace on the headers of the output string you're generating.

    You need to apply it to the text in this section of your code (within the for loop):

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

    which should be

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

    Because I can't see your full output file, I'm not certain that this is the issue you're seeing.