Search code examples
unicodelazarusfreepascal

Find the position of a unicodechar in a unicodestring


I'm working with Unicode. I want to find the position of a char in a UnicodeString.

For example: the position of á in lùáxõ is 3.

I have tried several ways, Google'd a lot, read the Lazarus documentation, but still nothing works.

I'm using Lazarus 1.6 and FPC 3.x.


Solution

  • Use pos() as David says, but to avoid problems with overloading make sure both arguments are explicitly typed unicode*

    E.g. copy and paste this in notepad, and save with utf-8 encoding

    {$mode delphi}
    {$codepage utf8} // source encoding is utf8, just in case.
    
    var c : unicodechar;
        s : unicodestring;
        i : Integer;
    begin
      s:='lùáxõ';
      c:='á'; // or whatever codepoint value of the char is.
      i:=pos(c,s);
      writeln(i);
    end.