Re: Recommendation on How to build a long string
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 26 Mar 2007 09:35:58 -0700
On Mar 26, 2:20 am, Göran Andersson <g...@xxxxxxxxx> wrote:
Mo wrote:
Hi,
I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?
character Position
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
FirstName LastName Address
street City st Zip xxx xxxxx xxxx
Thanks,
Mo
Use the StringBuilder to build the string from left to right, instead of
trying to creating a string and put things into it.
Example:
StringBuilder record = new StringBuilder();
record.Append(firstName);
record.Append(' ', 40 - record.Length);
recoRd.Append(lastName);
record.Append(' ', 80 - recoed.Length);
...
string result = record.ToString();
This looked promising until I noticed the following.
1. StringBuilder.Append doesn't have an Append signature for (string,
int). It does, however, have (string, int, int), where the first int
is the start location in the string, so, Göran's code is easily
changed:
StringBuilder record = new StringBuilder();
record.Append(firstName);
record.Append(' ', 0, 40 - record.Length);
record.Append(lastName);
record.Append(' ', 0, 80 - record.Length);
....
string result = record.ToString();
However, this still doesn't work, because if firstName or lastName is
longer than the maximum length, then the resulting record will be
misaligned, and the second int argument, the number of characters to
append, will be negative. The latter will cause Append to throw an
ArgumentOutOfRange exception.
So, you need to truncate the strings (if necessary), and _then_ do the
Append:
StringBuilder record = new StringBuilder();
if (firstName.Length > 40) firstName = firstName.Substring(0, 40);
record.Append(firstName);
record.Append(' ', 0, 40 - record.Length);
if (lastName.Length > 80) lastName = lastName.Substring(0, 80);
record.Append(lastName);
record.Append(' ', 0, 80 - record.Length);
....
string result = record.ToString();
.
- Follow-Ups:
- Re: Recommendation on How to build a long string
- From: Göran Andersson
- Re: Recommendation on How to build a long string
- References:
- Recommendation on How to build a long string
- From: Mo
- Re: Recommendation on How to build a long string
- From: Göran Andersson
- Recommendation on How to build a long string
- Prev by Date: Re: Confused by overloading (or can I claim it's a compiler bug...?)
- Next by Date: Re: SuspendLayout Not Suspending...
- Previous by thread: Re: Recommendation on How to build a long string
- Next by thread: Re: Recommendation on How to build a long string
- Index(es):
Relevant Pages
|