First Last - 1 year ago 186
Pascal Question

# How do I copy an array of ANSI strings so that I can get another array of ANSI string

I want to know if there is a function that slices an

`Array of AnsiString`
and return
`Array of AnsiString`
. The
`Copy`
function returns
`Array of String`
, so I happened to search online for another solution but I couldn't find anything.

Do we have a solution to this?

I'm using Free Pascal.

Copy will copy any `array of X` to another `array of X.` If `X` is `AnsiString`, it will copy (or slice) to an `array of AnsiString`.

The following proof project shows that the result is an `array of AnsiString`, as it addresses each individual "character". If the result were a `UnicodeString`, you would see only the first character (e.g. `'One'` would be the single "bytes" `'O'#0'n'#0'e'#0#0#0`, so the loop would stop after the `'O'`).

``````program Project1;

uses
SysUtils;

type
TAnsiStringArray = array of AnsiString;

procedure Main;
var
A, B: TAnsiStringArray;
I: Integer;
P: PAnsiChar;
begin
A := TAnsiStringArray.Create('Zero', 'One', 'Two', 'Three');

{ Plain Copy, nothing special: }
B := Copy(A, 1, 2);  { Copies 'One' and 'Two' }

for I := Low(B) to High(B) do
begin
P := PAnsiChar(B[I]);
while P^ <> #0 do
begin
Write(P^, '-');
Inc(P);
end;
Writeln;
end;
end;

begin
Main;
end.
``````

Output:

``````O-n-e-
T-w-o-
``````

So there is no need to do anything special. Just use `Copy` as it is.

FPC 3.0.0 on Windows.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download