Delphi - find the nature of a given position / index

I searched everything for this. In Delphi / Lazarus, given the position, I want to find the character in this position on another line. I know how to find a characterโ€™s position. I need it the other way around: a character in a given position. Thanks in advance.

+7
source share
4 answers

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

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

Access to the string can be like an array.

MyString [12] gives you the 12th character in the string. Note. This is the 1st index (since the 0th position is used to store the length of the string)

Example:

 var MyString : String; MyChar : Char; begin MyString := 'This is a test'; MyChar := MyString[4]; //MyChar is 's' end; 
+5
source

This is the last answer in 2012, so I decided to add an update:

For the latest version of Delphi (currently Tokyo Edition, which runs on multiple platforms using the FMX framework), the StringHelper class offers cross-platform character indexing. This implementation assumes a 0-based index for all supported platforms.

eg.

 var myString: String; myChar: Char; begin myChar := myString.Chars[0]; end; 
+1
source
 // 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; 
0
source

All Articles