problem z pętlą

0

mam taki kod:

var
  i: smallint;
  s: string;
  text: string;
begin

text := 'Name: Peter Quistgard; Seria';

if not Canvas.TextWidth(text) <= Main.lastmsg.Width then
begin
  for i := length(text) downto 0 do
  begin
    s := Copy(text, 0, i);
    if not (Canvas.TextWidth(s) <= 121) then
      Continue
    else
      text := s + '...';
  end;
end;

no i jest problem bo gdy chce to zrobić:

s := Copy(text, 0, i);

to mi za 'i' podstawia 0. a powinno być 'i' długość zmiennej 'text' (oczywiście za każdym razem zmiejszana). co mam robic?
wypróbujcie ten kod.

0
var
   i: smallint;
   s: string;
   text: string;
begin

text := 'Name: Peter Quistgard; Seria';

if not Canvas.TextWidth(text) <= Main.lastmsg.Width then
begin
   for i := length(text) downto 0 do
   begin
      s := Copy(text, 0, i);
      if not (Canvas.TextWidth(s) <= 121) then
         Continue
      else begin
         text := s + '...';
         Break;
      end;
   end;
end;
0

Taki mały hint: stringi indeksuje się od 1, nie od 0, więc jak chcesz skopiowac pierwszy znak, to copy(string, 1, 1).

1 użytkowników online, w tym zalogowanych: 0, gości: 1