Delphi - find character a given position/index

Viewed 20213

I have searched all over for this. In Delphi/Lazarus, given a position, I want to find the character at that position in a different string. I know how to find the position of a character. I need it the other way around: the character at a given position. Thanks in advance.

4 Answers
// AIndex: 0-based
function FindCharactedOfStringFromIndex(const AString: String; const AIndex: Integer): Char;
const
  {$IFDEF CONDITIONALEXPRESSIONS}
    {$IF CompilerVersion >= 24}
    STRING_FIRST_CHAR_INDEX = Low(AString);
    {$ELSE}
    STRING_FIRST_CHAR_INDEX = 1;
    {$ENDIF}
  {$ELSE}
  STRING_FIRST_CHAR_INDEX = 1;
  {$ENDIF}
var
  index: Integer;
begin
  index := STRING_FIRST_CHAR_INDEX + AIndex;
  Result := AString[index];
end;
Related