Recent

Author Topic: [SOLVED] The return value of Pos - what is the index value a measure of?  (Read 2546 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 700
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
« Last Edit: August 06, 2012, 02:15:40 pm by tedsmith »
Lazarus 2.0.4 and fpc 3.0.4 - Linux Mint 19 LTS, Windows 10 64 and Mac OSX Catlina
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: The return value of Pos - what is the index value a measure of?
« Reply #1 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.

Gizmo

  • Hero Member
  • *****
  • Posts: 700
Re: The return value of Pos - what is the index value a measure of?
« Reply #2 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.
Lazarus 2.0.4 and fpc 3.0.4 - Linux Mint 19 LTS, Windows 10 64 and Mac OSX Catlina
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

Leledumbo

  • Hero Member
  • *****
  • Posts: 8222
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: The return value of Pos - what is the index value a measure of?
« Reply #3 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"

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: The return value of Pos - what is the index value a measure of?
« Reply #4 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