Format fixed length fields with leading zeros or spaces

R

Rowan

Hi,

I am somewhat new to .net and c#. (What I learned in previous co has to
be unlearned). I am doing something that seems simple but I think there
is a better way than how I learned to do it. I am creating a fixed
length file with fixed length fields. Each field either requires
leading zeros or added on spaces. I learned to create a helper class
and for every field do this:
public static string FormatFieldA(string str12)
{
if (str12 == null || str12.Trim() == "")
//Default spaces required length = 12
{
return " ";
}
else
{
Int32 l = 12 - str12.Trim().Length;
if (l != 0)
for (int i = 1; i <= l; i++)
{
str12 = str12 + " ";
}
return str12;

}
}
I think there must be a better way like specifying in the Header or
Detail class for each field the field length and whether leadingzero is
t/f and then pass each field with those values to a simple format
function. But I don't know if that is the best way and I don't know how
to assign those constants. Could someone help? I want to do this well.
 
J

Jon Skeet [C# MVP]

Rowan said:
I am somewhat new to .net and c#. (What I learned in previous co has to
be unlearned). I am doing something that seems simple but I think there
is a better way than how I learned to do it. I am creating a fixed
length file with fixed length fields. Each field either requires
leading zeros or added on spaces. I learned to create a helper class
and for every field do this:

I think there must be a better way like specifying in the Header or
Detail class for each field the field length and whether leadingzero is
t/f and then pass each field with those values to a simple format
function. But I don't know if that is the best way and I don't know how
to assign those constants. Could someone help? I want to do this well.

One way would be to have a custom attribute describing each field.
Fetch the attributes at execution time and call formatting methods
appropriately. Alternatively you could use an interface to specify (on
each class) what the field length should be.

One thing to point out though: String.PadRight and String.PadLeft are
your friends :)
 
Z

zacks

Hi,

I am somewhat new to .net and c#. (What I learned in previous co has to
be unlearned).  I am doing something that seems simple but I think there
is a better way than how I learned to do it.  I am creating a fixed
length file with fixed length fields.  Each field either requires
leading zeros or added on spaces.  I learned to create a helper class
and for every field do this:
       public static string FormatFieldA(string str12)
        {
            if (str12 == null || str12.Trim() == "")
            //Default spaces required length = 12
            {
                return "            ";
            }
            else
            {
                Int32 l = 12 - str12.Trim().Length;
                if (l != 0)
                    for (int i = 1; i <= l; i++)
                    {
                        str12 = str12 + " ";
                    }
                return str12;

            }    
        }
I think there must be a better way like specifying in the Header or
Detail class for each field the field length and whether leadingzero is
t/f and then pass each field with those values to a simple format
function.  But I don't know if that is the best way and I don't know how
to assign those constants. Could someone help?  I want to do this well.

Look into the String.Format method. One thing, you will need to change
the datatype of the value to be padded to a numeric datatype.

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I am somewhat new to .net and c#. (What I learned in previous co has to
be unlearned).  I am doing something that seems simple but I think there
is a better way than how I learned to do it.  I am creating a fixed
length file with fixed length fields.  Each field either requires
leading zeros or added on spaces.  I learned to create a helper class
and for every field do this:
       public static string FormatFieldA(string str12)
        {
            if (str12 == null || str12.Trim() == "")
            //Default spaces required length = 12
            {
                return "            ";
            }
            else
            {
                Int32 l = 12 - str12.Trim().Length;
                if (l != 0)
                    for (int i = 1; i <= l; i++)
                    {
                        str12 = str12 + " ";
                    }
                return str12;

            }    
        }
I think there must be a better way like specifying in the Header or
Detail class for each field the field length and whether leadingzero is
t/f and then pass each field with those values to a simple format
function.  But I don't know if that is the best way and I don't know how
to assign those constants. Could someone help?  I want to do this well.

*** Sent via Developersdexhttp://www.developersdex.com***


Take a look at PadLeft/PadRight.

In your case can be myFieldValue.ToString().PadLeft( sizeOfField,
charToFillWith);
 
R

Rowan

Okay, the padding part makes sense. I am a little lost when it comes to
the custom attributes. I think I would need

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]

Where does this go? Does it get it's own class like public class
MyFieldsAttribute or because it is field level does it go in the class I
am using the attributes for? I need an attribute for requiredlength
(int) and leadingzeros (bool)...where does it go? I am finding a lot to
read about it but not quite understanding where everything goes...help
is very greatly appreciated.
 
J

Jon Skeet [C# MVP]

Rowan said:
Okay, the padding part makes sense. I am a little lost when it comes to
the custom attributes. I think I would need

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]

Where does this go? Does it get it's own class like public class
MyFieldsAttribute or because it is field level does it go in the class I
am using the attributes for?

The attribute type itself is just a class, even though it's applied to
fields. I wouldn't make it a nested class.
I need an attribute for requiredlength
(int) and leadingzeros (bool)...where does it go? I am finding a lot to
read about it but not quite understanding where everything goes...help
is very greatly appreciated.

See if http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx
helps you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top