La forma habitual de recorrer una cadena, al menos para mi, es usar un bucle for
Código Delphi
[-]
var
i: integer;
s: string;
begin
s:= 'Hola mundo';
for i:=1 to Length(s) do
begin
ShowMessage(s[i]);
end;
end;
Si tiene que ser forzosamente con un bucle while:
Código Delphi
[-]
var
i: integer;
s: string;
begin
s:= 'Hola mundo';
i:= 0;
while i < Length(s) do
begin
inc(i);
ShowMessage(s[i]);
end;
end;
Incluso podemos usar un repeat
Código Delphi
[-]
var
i: integer;
s: string;
begin
s:= 'Hola mundo';
i:= 1;
if i <= Length(s) then
repeat
ShowMessage(s[i]);
inc(i);
until i > Length(s);
end;
A partir de ahí, podemos usar toda una serie de métodos a cada cual mas
exótico. Por ejemplo:
Código Delphi
[-]
var
p: PChar;
s: string;
begin
s:= 'Hola mundo';
p:= PChar(s);
while Boolean(P^) do
begin
ShowMessage(Char(P^));
inc(P);
end;
end;
O incluso podemos destruir la cadena según la procesamos
Código Delphi
[-]
var
s: string;
begin
s:= 'Hola mundo';
while Length(s)>0 do
begin
ShowMessage(s[1]);
delete(s,1,1);
end;
end;
Y podriamos continuar asi durante un rato ....