Link to home
Create AccountLog in
Avatar of wademi

asked on

In how do I specify the exact number of spaces between strings/fields in a text file.

I am writing to a text file with 5 fields in a row as shown in the code below. I would like to specify the exact number of spaces where a field should start in a row.

For example Field1 is a date field that should start at 0 space from left to right.
Field2 starts at the 9th space from left to right in row 1.
Field3 starts at the 17th space from left to right in row 1
Field 4 starts at the 20th space row 1

and so on..............

I was using streambuilder  to append the strings in a row as shown in the code below but I do not want to specify the spaces between fields with double quotes followed by space("     ").
I need a more efficient way to specify the exact number of spaces where a field should start.

StreamWriter sw = new StreamWriter(location);
            StringBuilder sb = new StringBuilder();

            // Format the text file and write it to C:\Formated.txt
            foreach (string line in lines)
                string[] fields = line.Split(new char[] { ',' });
                string dt = DateTime.Parse(fields[0]).ToString("yyyyMMdd");
                sb.Append(dt + fields[1] + "     " + "03" + "                      " );
                string amount = fields[3];
                for (int i = amount.Length; i < 38; i++) amount += " ";
                sb.Append(amount + fields[4].ToUpper());
                sb.Length = 0;

Avatar of ALaRiva
Flag of United States of America image

Use the PadRight() method of the String.

string str = "fke";
str.PadRight(9,' ');

That would pad the right side with whatever character in the second argument, in this case, a space.

fke------       (imagine the - is a space)

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Funny. I had a nice, neat solution to this in your last question. Maybe you didn't get that far in reading it  ;)
Avatar of wademi


Hi Guys. Thanks for your helpThe suggestion by FernandoSoto: and ALaRiva: were very helpful.
However the pad right option may lead to fields beign included in the incorrect column if the field with the pad right is shorter or longer in length.

Here is the spec of the file I am trying to format:

Chracters from left                   Field as in example above
001-008                                    DT
009-016                                    fields[1]
017-019                                    "03"
062-082                                    amount
083-085                                    fields[4]

So the DTfield occupies the first 8 characters from left to right in the text file then fields[1] has to start at the 9th character then  "03" from the 17th charcter and so on.
The pad right option would work if the fields will have the same amount of characters every single time. But in this case they wont.

Should I be using the string.format() method.  How can i absolutely ensure that the fields start in the exact characters spaces as suggested by the spec file?
String.PadRight(int32) will return a string with a total field width defined in its parameter. If a string to be padded has more characters then the last time the number of spaces is reduced so that the total field width equals the integer parameter value of PadRight, so the field width is always the same size and if the characters to be padded exceeds the field width it will be truncated to the defined field width so that the width is always the same. In your above example the column positions have changed from the original question or are not accounting for them

Chracters from left                   Field as in example above
001-008                                    DT
009-016                                    fields[1]
017-019                                    "03"
020-061    What happens here? it goes from column 19 to 62?
062-082                                    amount
083-085                                    fields[4]