Delphi - find character a given position/index

preahkumpii picture preahkumpii · Jul 18, 2012 · Viewed 16.7k times · Source

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.

Answer

Hemant picture Hemant · Jul 18, 2012

In Delphi, a character in the string can be indexed using the array notation. Just note that first character in string has an index of one.

var
  s: string;
  c: char;
begin
  s := 'Hello';
  c := s[1]; //H
end;