Author Topic: Why copy() bad work  (Read 930 times)

0 Members and 1 Guest are viewing this topic.

Offline soldat-game

  • Camper
  • ***
  • Posts: 402
Why copy() bad work
« on: July 24, 2016, 07:01:15 am »
Hello copy dont work for me if im use me function this return me fasola do remi why not fasola?
if im use text[pos2] this return good letter fasol>>>a<<<
Code: [Select]
function GetPiece2(text,pos:string;num:integer):string;
var i,findpos,pos1,pos2:integer;
begin
findpos:=0;
Result:='';
for i := 1 to length(text) do begin
if text[i]=pos then begin
if num=0 then begin
Result:=Copy(text,1,i-1);
exit;
end else
begin
findpos:=findpos+1;
if findpos=num then pos1:=i+1;
if findpos=num+1 then begin
pos2:=i-1; //debug
WriteLn(inttostr(pos2)+' '+text[pos2]); //debug
Result:=Copy(text,pos1,i-1);
exit;
end;
end;
end else
begin
if i=length(text) then begin
Result:=Copy(text,pos1,length(text));
exit;
end;
end;
end;
end;

procedure ActivateServer();
var b: byte; Temp: TStringList; stong:string;
begin
stong := 'Do remi fasola do remi';
WriteLn(GetPiece2(Stong,' ',2)+'-TEST!');

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Why copy() bad work
« Reply #1 on: July 24, 2016, 12:00:15 pm »
2nd argument to Copy is length, not last index. It should be i - pos1 - 1.
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.