Lazarus

Programming => General => Topic started by: Gizmo on August 06, 2012, 01:54:28 pm

Title: [SOLVED] The return value of Pos - what is the index value a measure of?
Post by: Gizmo on August 06, 2012, 01:54:28 pm
Pos "returns the index of Substr in S, if S contains Substr".

http://www.freepascal.org/docs-html/rtl/system/pos.html

So, if

FoundAt := IntToStr(Pos('I am looking for', String));

returns a hit reported as "11", what does that mean? It obviously means "Index 11" but I don't understand what the index is a count of? 11 lines? 11 bytes? Offset 11 as decimal?

Many thanks

Ted
Title: Re: The return value of Pos - what is the index value a measure of?
Post by: User137 on August 06, 2012, 02:01:46 pm
Code: [Select]
var s: string;
begin
  s:='I am looking for';
  //s[11] would be "n", or same as copy(s, 11, 1)
end;
It means 11th byte (or char type) in string.

But note that string is UTF8 encoded, some 1 special character may take even 4 bytes on its own. For those cases there are UTF8Pos() and other functions.
Title: Re: The return value of Pos - what is the index value a measure of?
Post by: Gizmo on August 06, 2012, 02:15:28 pm
I feel like an idiot. That was a question of utter stupidity. If I had spent 5 seconds thinking about it I would have just used a ShowMessage to show 'String' and I'd have realised it was the 11th value. Index...as in String[0..XX]....yeah. Sory for wasting your time and thanks for the explanation.
Title: Re: The return value of Pos - what is the index value a measure of?
Post by: Leledumbo on August 06, 2012, 02:16:08 pm
Quote
It obviously means "Index 11" but I don't understand what the index is a count of? 11 lines? 11 bytes? Offset 11 as decimal?
11 as in "11th character of the string"
Title: Re: The return value of Pos - what is the index value a measure of?
Post by: User137 on August 06, 2012, 03:43:23 pm
I feel like an idiot. That was a question of utter stupidity. If I had spent 5 seconds thinking about it I would have just used a ShowMessage to show 'String' and I'd have realised it was the 11th value. Index...as in String[0..XX]....yeah. Sory for wasting your time and thanks for the explanation.
Krhm... String[1..XX]  :P
TinyPortal © 2005-2018