Board index » delphi » Newbie ?: array of null-term. chars

Newbie ?: array of null-term. chars

This question probably has a simple answer, but...

how do i take an array of null-terminated strings and extract each individual
string? For example, I have an array that contains this:

STRING1<null>STRING2<null>STRING3<null>

and let's say I want to extract the individual strings and append them as items
in a listbox or memo. How do I do this? been reading the help file for about an
hour now on string manipulation and stuff like that but can find no answer.

Any help is appreciated.

=)

--
--------------------------------------------------
http://pirchworld.com
--------------------------------------------------

 

Re:Newbie ?: array of null-term. chars


On Sat, 14 Apr 2001 21:57:45 -0400, "Jellowe B. Afreyd"

Quote
<jell...@pirchworld.com> wrote:
>This question probably has a simple answer, but...

>how do i take an array of null-terminated strings and extract each individual
>string? For example, I have an array that contains this:

>STRING1<null>STRING2<null>STRING3<null>

>and let's say I want to extract the individual strings and append them as items
>in a listbox or memo. How do I do this? been reading the help file for about an
>hour now on string manipulation and stuff like that but can find no answer.

>Any help is appreciated.

The simplest approach (assuming you know how many items are in the
array or how long the entire array is, so you don't access memory you
oughtn't) is to use a PChar and increment it as needed.

The trick here is to realize that most of the PChar based string
handling routines stop at the first null character they encounter; but
you can also manipulate the PChar directly to bypass them.

Assuming the array you describe is in MyCharArray, something like this
should work:

var
  P : PChar;
  I : Integer;
begin  
  P := @MyCharArray;  // start of first string

  for I := 1 to HowEverManyStringsThereAre do begin  
    MyStringList.Add(StrPas(P));
    Inc(P, StrLen(P) + 2);   // increment past null terminator
  end;
end;

Do you see how that works?

Sometimes structures such as this are double null terminated at the
end; for this kind a test that P doesn't point to a null is in order
to stop the loop, in which case you'd probably want a while or
repeat..until loop rather than a for loop.

Stephen Posey
slpo...@concentric.net

Re:Newbie ?: array of null-term. chars


"Jellowe B. Afreyd" <jell...@pirchworld.com> wrote in message
news:3AD90019.D07C1A1B@pirchworld.com...

Quote
> This question probably has a simple answer, but...

> how do i take an array of null-terminated strings and extract each
individual
> string? For example, I have an array that contains this:

> STRING1<null>STRING2<null>STRING3<null>

> and let's say I want to extract the individual strings and append them as
items
> in a listbox or memo. How do I do this? been reading the help file for
about an
> hour now on string manipulation and stuff like that but can find no

answer.

procedure AddToTStrings (theStrings : array of char; theTStrings :
tStrings);

var pStart : pChar;
    i : integer;

begin
pStart := @theStrings;
while (pStart - theStrings) <= High (theStrings) do
     begin
     theTStrings.Add (String (pStart));
     pStart := pStart + Length (pStart) + 1;
     end;
end;

Other Threads