Janilabo Posted October 5, 2012 Share Posted October 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
LordJashin Posted October 5, 2012 Share Posted October 5, 2012 Nice this is what I was looking for when it comes to that Copy function! Quote Link to comment Share on other sites More sharing options...