Jump to content
bk31415

Why Pos() and Lines.SelStart differ?

Recommended Posts

This is sample code:

procedure TForm1.Button1Click(Sender: TObject);
const
  CRLF = #13#10;
var
  Text1: String;
  Pos1: Integer;
begin
  Text1 := '[' + CRLF;
  Text1 := Text1 + '{' + CRLF;
  Text1 := Text1 + '"country":"AD",' + CRLF;
  Text1 := Text1 + '"name":"Sant Julià de Lòria",' + CRLF;
  Text1 := Text1 + '"lat":"42.46372",' + CRLF;
  Text1 := Text1 + '"lng":"1.49129"' + CRLF;
  Text1 := Text1 + '},' + CRLF;
  Text1 := Text1 + '{' + CRLF;
  Text1 := Text1 + '"country":"AD",' + CRLF;
  Text1 := Text1 + '"name":"Pas de la Casa",' + CRLF;
  Text1 := Text1 + '"lat":"42.54277",' + CRLF;
  Text1 := Text1 + '"lng":"1.73361"' + CRLF;
  Text1 := Text1 + '},' + CRLF;
  Text1 := Text1 + '{' + CRLF;
  Text1 := Text1 + '"country":"AD",' + CRLF;
  Text1 := Text1 + '"name":"Ordino",' + CRLF;
  Text1 := Text1 + '"lat":"42.55623",' + CRLF;
  Text1 := Text1 + '"lng":"1.53319"' + CRLF;
  Text1 := Text1 + '},' + CRLF;
  Text1 := Text1 + '{' + CRLF;
  Text1 := Text1 + '"country":"AD",' + CRLF;
  Text1 := Text1 + '"name":"les Escaldes",' + CRLF;
  Text1 := Text1 + '"lat":"42.50729",' + CRLF;
  Text1 := Text1 + '"lng":"1.53414"' + CRLF;
  Text1 := Text1 + '}' + CRLF;
  Text1 := Text1 + ']'; {26 lines}

  Pos1 := Pos('Ordino', Text1);
  Memo1.Lines.Text := Text1;
  Memo1.SelStart := Pos1;
  Memo1.SelLength := Length('Ordino');
  Memo1.SetFocus;
end;

When I run this, I expect Memo1.SelText to be 'Ordino' but the actual selected text turns out to be 'rdino"'.

 

I am baffled why it shifts 1 char right.

 

Could someone explain why.

Edited by bk31415

Share this post


Link to post

I led myself to think the reason to be more complex --like count of CRLF's etc.

But, 0/1-base issue never occured to me.

Thank you.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×