locked
StringBuilder RRS feed

  • Question

  • User-1165521811 posted

    Hey all,

     I'm attempting to use Stringbuilder to write a fixed length text file. However, I can't seem to get the spacing right. Essentially, I need 2 spaces for the first field, 9 for the second, 18 for the third, etc, etc. The point being I need to preserve the spaces no matter how long the varaible. Any ideas would be greatly appreciated.

    Here's what I have thus far:

    Dim strBuilder As New StringBuilder(310)
            strBuilder.AppendFormat("{0,2}", strComp)
            strBuilder.AppendFormat("{0,9}", strEmpNo)
            strBuilder.AppendFormat("{0,18}", strEmpLname)
            strBuilder.AppendFormat("{0,11}", strEmpFname)
            strBuilder.AppendFormat("{0,1}", strMinit)

            CreateFile(strBuilder)

    Flat file looks like this:

    20        123456789            LEORGE    WILLIAMA

    Thursday, June 11, 2009 3:12 PM

Answers

  • User271624304 posted

    It's working fine in console application, please check in your CreateFile Function if it's working fine, if not, please provide code for the same.

     

    StringBuilder strBuilder = new StringBuilder(310);

    strBuilder.AppendFormat("{0,2}", "20");

    strBuilder.AppendFormat("{0,9}", "123456");

    strBuilder.AppendFormat("{0,18}", "Naimish");

    strBuilder.AppendFormat("{0,11}", "Pandya");

    strBuilder.AppendFormat("{0,10}", "Mumbai");

    Console.WriteLine(strBuilder);

    Console.ReadLine();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2009 5:02 AM

All replies

  • User-925286913 posted

    Have you tried to replace spaces with   while displaying it as HTML?

    Or show it within <em> tag.

    Friday, June 12, 2009 3:01 PM
  • User-925286913 posted

    Have you tried to replace spaces with &nbsp; while displaying it as HTML?

    Or show it within <pre> tag.

    Friday, June 12, 2009 3:02 PM
  • User271624304 posted

    It's working fine in console application, please check in your CreateFile Function if it's working fine, if not, please provide code for the same.

     

    StringBuilder strBuilder = new StringBuilder(310);

    strBuilder.AppendFormat("{0,2}", "20");

    strBuilder.AppendFormat("{0,9}", "123456");

    strBuilder.AppendFormat("{0,18}", "Naimish");

    strBuilder.AppendFormat("{0,11}", "Pandya");

    strBuilder.AppendFormat("{0,10}", "Mumbai");

    Console.WriteLine(strBuilder);

    Console.ReadLine();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2009 5:02 AM
  • User-1165521811 posted

    Thanks for the replies. I'm not exactly sure why this method didn't work, but I ended up using the StringBulder.Insert method to build the text file. This allowed me to define the length of each field and apply conversion functions where needed.

    Dim strBuilder As New StringBuilder()
            strBuilder.Insert(0, strCompany)
            strBuilder.Insert(2, strEmpNo)
            strBuilder.Insert(11, strEmpLastName)
            strBuilder.Insert(29, strEmpFirstname)
            strBuilder.Insert(40, strMidinit)
            strBuilder.Insert(41, (ConvertDate(strDate)))

    CreateFile(strBuilder)

    Again, thanks for the replies. I appreciate the time and effort.

    Monday, June 15, 2009 2:16 PM
  • User271624304 posted

    The code I have provided it's working fine for me, even your code works fine...Enjoy :)

    Monday, June 15, 2009 9:56 PM