Author Topic: [SOLVED] TStringHelper.LastIndexOf() with large String returns -1  (Read 571 times)


  • New Member
  • *
  • Posts: 35
Hi. More than 10 years passed since last time I tried creating GUI stuff with Lazarus as a kid...

I'm trying to deserialize a pretty large (>1000 chars) string, but LastIndexOf() seems to be joking on me:

Code: Pascal  [Select][+][-]
  1. var FieldStart, FieldEnd: SizeInt;
  2. begin
  3.     FieldName := 'Timetable'; // actually it's a function argument
  4.     FieldStart := S.IndexOf(FieldName + '=');
  5.     if FieldStart < 0 then exit;
  6.     FieldEnd := S.LastIndexOf(';', FieldStart);
  7.     if FieldEnd < 0 then exit;

And the string S:

FieldStart is set correctly to 0, but for some reason FieldEnd is -1. The char I'm looking for is literally the last one.

FPC 3.2.2, Lazarus 3.4
« Last Edit: June 13, 2024, 04:53:51 am by trexet »


  • New Member
  • *
  • Posts: 35
Re: TStringHelper.LastIndexOf() with large String returns -1
« Reply #1 on: June 13, 2024, 04:53:01 am »
Should've read the docs with more attention:

The search can be refined by specifying a (zero based) index StartIndex. When specified, the search will start at the given character and proceeds towards the beginning of the string.

For some reason I supposed that StartIndex works the same as for IndexOf().


  • Hero Member
  • *****
  • Posts: 2287
Re: TStringHelper.LastIndexOf() with large String returns -1
« Reply #2 on: June 13, 2024, 06:49:34 am »
For some reason...
Maybe the Delphi documentation is confusing:
"StartIndex specifies the offset in this 0-based string where the LastIndexOf method begins the search, and Count specifies the end offset where the search ends".

As far as I know, in Delphi, as in other languages, the search is performed from the end (or StartIndex) to the beginning.


TinyPortal © 2005-2018