Jump to content
Janilabo

StrCopy

Recommended Posts

This function copies (partial) string from str - copy process starts at pos1 and ends to pos2.

Works both ways, Low=>High (normal copy) and High=>Low (reversed copy).

 

Included an example. :)

 

const
 TEST_STR = 'Janilabo is a nub! ';
 START_POS = 1;
 END_POS = 21; 

function StrCopy(str: string; pos1, pos2: Integer): string;
var
 i, l: Integer;
begin
 l := Length(str);
 if (l > 0) then 
 begin     
   if (pos1 < 1) then
     pos1 := 1;
   if (pos1 > l) then
     pos1 := l;
   if (pos2 < 1) then
     pos2 := 1;
   if (pos2 > l) then
     pos2 := l;
   case (pos1 < pos2) of
     False:
     if (pos1 <> pos2) then
     begin
       SetLength(Result, ((pos1 - pos2) + 1));
       for i := pos1 downto pos2 do
         Result[((pos1 - i) + 1)] := str[i]; 
     end else
       Result := Copy(str, pos1, 1);
     True: Result := Copy(str, pos1, ((pos2 - pos1) + 1)); 
   end;
 end;
end;

begin
 ClearDebug;
 WriteLn('StrCopy(''' + TEST_STR + ''', ' + IntToStr(START_POS) + ', ' + IntToStr(END_POS) + '): ' + #13#10 + StrCopy(TEST_STR, START_POS, END_POS) + ' [Low=>High]');
 WriteLn('');
 WriteLn('StrCopy(''' + TEST_STR + ''', ' + IntToStr(END_POS) + ', ' + IntToStr(START_POS) + '): ' + #13#10 + StrCopy(TEST_STR, END_POS, START_POS) + ' [High=>Low]');
end.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...