Re: Recommendation on How to build a long string

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
012345678901234567890123456789012345678901234567890123456789012345678901234­567890123456789
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();

.



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Gcc compatible header file
    ... A string collection is a table of zero terminated strings that will grow ... typedef struct _StringCollection StringCollection; ... int; ... bool; ...
    (comp.lang.c)
  • Kernighan and Pikes "Beautiful" Code
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • Re: FTP CD command
    ... public const int GENERIC_WRITE = 0x40000000; ... string lpszProxyName, ... public static extern IntPtr InternetConnect ( ... public static extern bool FtpGetCurrentDirectory ( ...
    (microsoft.public.dotnet.languages.vb)