473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Format fixed length fields with leading zeros or spaces


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 Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
5 3420
Rowan <an***********@yahoo.comwrote:
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:
<snip>
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 :)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
On May 14, 2:30*pm, Rowan <anonymous1...@yahoo.comwrote:
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
Jun 27 '08 #3
On May 14, 2:30*pm, Rowan <anonymous1...@yahoo.comwrote:
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);
Jun 27 '08 #4
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.

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #5
Rowan <an***********@yahoo.comwrote:
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/libr...54(VS.71).aspx
helps you.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
1
by: Andrew | last post by:
I run a fixed-width export based on a query, and the query looks like this... Clients.SSN, Clients.ClientID, ServiceEvents.SeviceDate, CStr(Format(,"0000")) AS Servs, "28" AS SETTING, FROM...
3
by: John B | last post by:
I'm receiving a fixed length string with fixed length fields that I need to break apart. Ideally I'd like to take the string and put it into a Structure and then access the individual fields from...
5
by: David Garamond | last post by:
The MySQL manual recommends that we create a "fixed-length row" if possible, for speed (especially scanning speed). A fixed-length row is a row which is comprised of only fixed-length fields. A...
2
by: Frank Swarbrick | last post by:
I'm just learning about embedded SQL, so be gentle... My basic question is, if I use a fixed length host variable for a column defined as VARCHAR, will trailing spaces be removed (or not) upon...
1
by: htres | last post by:
Hello, I'm new to programming and C#, so please bear with my ignorance! I need to extract jpeg images and header data from a binary file. The binary file is formatted with several fixed...
6
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for...
4
by: Scotty | last post by:
Hi everyone, Is there an equivalent to vb6 Dim test as String * 40 ? I have been looking for a solution but no luck until now. Any help would be greatly appreciated.
0
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.