Hi,

I have a function that removes whitespace from a memo, but it dosn't work in VCL.NET. Something about unsafe code and invalid typecasts...

Could somebody rewrite it without using pointers? and adjust it to work on a stringlist instead of a memo.

procedure StripWhiteSpaces(AMemo: TMemo);

var p1,p2: pchar;

s: string;

i: integer;

begin

s:= AMemo.Lines.Text;

if s = '' then exit;

p1:= pchar(s);

p2:= p1;

while p2^ <> #0 do

begin

if (p2^ = ' ') AND ((pchar(p2+1)^ = ' ') or (pchar(p2-1)^ in [#13,#10]))

then Delete(s, p2 - p1 + 1,1)

else inc(p2);

end;

AMemo.Lines.Text:= trim(s);

for i:= AMemo.Lines.Count-1 downto 0 do

if AMemo.Lines[i] = ''

then AMemo.Lines.Delete(i);

end;

Thanks

for examnple the first one from alex:

function StripMultipleSpaces(const Value: string): string;

var

I, J: Integer;

Max: Integer;

begin

Result := Value;

I := Pos(#32#32, Result);

if (I > 0) then

begin

Max := Length(Result);

repeat

Inc(I);

J := Succ(I);

while (J <= Max) and (Result[J] = #32) do

Inc(J);

while (J <= Max) do

begin

if (Result[Pred(I)] = #32) then

begin

while (J <= Max) and (Result[J] = #32) do

Inc(J);

end;

if (J <= Max) then

begin

Result[I] := Result[J];

Inc(I);

Inc(J);

end;

end;

until (J > Max);

SetLength(Result, Pred(I));

end;

end;

call it like:

var s:tstrings;

begin

s.text:=StripMultipleSpace

or any other solution that doesn't use pointers.