0

I've run into a bit of an issue. One of my fields row {4} has a variable length of characters. However, its ending position in the file must be 23 characters from the end of row {3}. So I need to add however much white space is necessary to equal 23 characters for row {4}. As you can see, I've tried this with a for loop. Unfortunately it does not work. I'm not sure if it's the method I'm using, or the syntax that's the problem.

      if ((checkResp91 != null) || (checkResp00 != null))
            {
                string selectCommandText91 = "SELECT * FROM EFT_BANK_INFORMATION1";
                using (SqlDataAdapter adapter91 = new SqlDataAdapter(selectCommandText91, connection))
                {
                    using (DataTable table91 = new DataTable("EFT_BANK_INFORMATION1"))
                    {
                        adapter91.Fill(table91);
                        System.Text.StringBuilder commaDelimitedText = new System.Text.StringBuilder();
                        //commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
                        foreach (DataRow row in table91.Rows)
                        {
                            string spaceholder = "";
                            string spacer = row[4].ToString();
                            int countit = spacer.Length;
                            for (int i = countit; i < 23; i++)
                            {
                                spaceholder = spaceholder + " ";
                            }

                            string value = string.Format("{0}{1}                {0}         {2}{3}{4}{spaceholder}{5}", row[1], row[2], row[3], row[4], spaceholder, row[5], row[6]); // how you format is up to you (spaces, tabs, delimiter, etc)
                            commaDelimitedText.AppendLine(value);
                        }
                        System.IO.File.WriteAllText("C:\\BillingExport\\tbl9_1_export.txt", commaDelimitedText.ToString());
                    }
                }
            }

Changed to

   //commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
                        foreach (DataRow row in table91.Rows)
                        {
                            string spacer = row[4].ToString();
                            string newvalue = (spacer + new string(' ', 23)).Substring(0, 23);
                            string value = string.Format("{0}{1}                {0}         {2}{3}{4}{5}", row[1], row[2], row[3], newvalue, row[5], row[6]); // how you format is up to you (spaces, tabs, delimiter, etc)
                            commaDelimitedText.AppendLine(value);
                        }
                        System.IO.File.WriteAllText("C:\\BillingExport\\tbl9_1_export.txt", commaDelimitedText.ToString());
                    }
                }

Which is giving me extra spaces for some reason, and putting them before the characters, when I need it after.

Output from the above code:

09                  09         011301390011111119              AAAAAAA AAAABBBBBBBB BBBBBBBBB CO.

when it needs to be

09                  09         011301390011111119AAAAAAA AAAA           BBBBBBBB BBBBBBBBB CO.
2
  • string value = string.Format("{0}{1}{2}{3}{4}{5}{6}", row[1], row[2], row[3], row[4], spaceholder, row[5], row[6]); Commented Aug 6, 2014 at 14:27
  • you have {spaceholder} in as a placeholder, instead of the numeric position in the argument list - {5}. Commented Aug 6, 2014 at 14:33

1 Answer 1

1

Trying to reinvent Fortran record files eh?

Just use argument lengths in your string.Format():

string.Format("{0}{1}                {0}         {2}{3}{4,23}{5}", row[1], row[2], row[3], row[4], row[5], row[6]);

I'd also use it to get rid of those spaces, but up to you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This worked, although it put the white space before the characters in the file, and I need it to be after.
@Dave, use {4,-23} if you want the other alignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.