Why these results with Pos? lazarus / 21-General ---------------------------------------------------------------------- geraldholdsworth · 2023-11-11 11:45 Just found a little quirk. If you use Pos(char,string) you get different results depending on what is in the string: Code: [Select]Position of xx in FLASH(£xx) is 9 Position of xx in FLASH(%xx) is 8 Position of xx in FLASH(?xx) is 8Why is the pound sign treated differently to others? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  { TForm1 }  TForm1 = class(TForm)  Memo1: TMemo;  procedure FormShow(Sender: TObject); private  public  end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormShow(Sender: TObject);var Tmp1: String;const Temp:array[0..2] of String = ('FLASH(£xx)','FLASH(%xx)','FLASH(?xx)');begin Memo1.Clear; for Tmp1 in Temp do  Memo1.Lines.Add('Position of xx in '+Tmp1+' is '+IntToStr(Pos('xx',Tmp1)));end; end.  (Lazarus 2.2.6 on macOS) ---------------------------------------------------------------------- Handoko · 2023-11-11 12:05 I think what you want is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Memo1.Lines.Add('Position of xx in '+Tmp1+' is '+IntToStr(Pos(UnicodeString('xx'),Tmp1))); AI is stupid, computer is stupid too. Sometimes you need to tell them what you mean/want exactly. . https://www.freepascal.org/docs-html/rtl/system/pos.html ---------------------------------------------------------------------- BeniBela · 2023-11-11 12:08 Quote from: geraldholdsworth on November 11, 2023, 12:45:53 pmWhy is the pound sign treated differently to others? U͚͚͚͚᷃᷃᷃n̺ͦi̼̼̼̼̼︡︡︡c̝̝̝︢o̘̚̚d̠̠̠̑̑̑̑̑e͈͠͠͠͠͠ ---------------------------------------------------------------------- paweld · 2023-11-11 12:09 This is because £ is a character outside the ASCII array, and its size is 2 bytes. To get correct results for texts containing characters outside ASCII, use the UTF8Pos function from the LazUTF8 module.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //...      uses           //...,           LazUTF8;     //...         for Tmp1 in Temp do    Memo1.Lines.Add('Position of xx in '+Tmp1+' is '+IntToStr(UTF8Pos('xx',Tmp1)));  Result:Code: [Select]Position of xx in FLASH(£xx) is 8 Position of xx in FLASH(%xx) is 8 Position of xx in FLASH(?xx) is 8 ---------------------------------------------------------------------- geraldholdsworth · 2023-11-11 12:13 Quote from: paweld on November 11, 2023, 01:09:58 pmThis is because £ is a character outside the ASCII array, and its size is 2 bytes. Thank you everyone. I did not know that. I've been brought up with £ being ASCII 96! ---------------------------------------------------------------------- Thaddy · 2023-11-11 16:17 Which it still is, depending on codepage. The problem is UTF8. ---------------------------------------------------------------------- MarkMLl · 2023-11-11 18:02 Quote from: Thaddy on November 11, 2023, 05:17:09 pmQuote from: geraldholdsworth on November 11, 2023, 01:13:47 pmThank you everyone. I did not know that. I've been brought up with £ being ASCII 96! Which it still is, depending on codepage. The problem is UTF8. I'm not sure which is more of a problem: codepages or UTF8. However I don't recall £ being at 96 (0x60) since this is normally ` (backtick etc.): IMO it's normally either overlaid # (ISO/IEC 646-GB) or has been somewhere "above the line" e.g. 0xa3. MarkMLl ---------------------------------------------------------------------- Bart · 2023-11-11 18:15 Cp437: 0x9C Cp850: NA cp852: NA IEC_8859-1: 0xA3 Bart ---------------------------------------------------------------------- tetrastes · 2023-11-11 20:34 Quote from: Bart on November 11, 2023, 07:15:49 pmCp850: NA ? cp850: 0x9C ---------------------------------------------------------------------- Warfley · 2023-11-11 20:56 Quote from: MarkMLl on November 11, 2023, 07:02:38 pmI'm not sure which is more of a problem: codepages or UTF8. Depends on what you are writing your applications for. If you write an application that should only work for a western country, 8bit character codepages are by far the easiest way. Of course this changes if you take for example chinese characters, where you have I think 90k defined currently in unicode, meaning not even UTF-16 can handle them without planes (which are totally not codepages, because unicode wanted to abolish code pages, and it would be stupid if they had to reintroduce them again). Even worse when you write code that must handle inputs from the whole world, which is why UTF-8 was mostly pushed from the web, because social media pages and so on are global. But UTF-8 is completely fine, you get a bit of a performance penalty as you always have to iterate to find specific characters (which why for parsers etc. I often convert the UTF-8 chunks to UTF-32 internally), the problem with Pascal is the historic mixing of different string encodings. You have the RTL functions like Pos, which treat the string as ANSI, the LCL which uses UTF-8, and to make things more complicated, with Delphi compatibility UTF-16. Together with the great naming where AnsiString means UTF-8 unless it doesn't, and UnicodeString means WideString, even though UTF-8 is as much Unicode as UTF-16 is. Languages which where developed after UTF-8 has "won" in the unicode encoding discussion, are much more transparent. Rust for example has UTF-8 strings, but UTF-32 chars, which the runtime will internally convert into each other meaning the programmer does not have to care with codepoints, codepoint length, etc. Go goes a similar route with it's "rune" type. JavaScript as the Web language, where everything is UTF-8, simply doesn't have a char type at all, but only UTF-8 strings. Backwards compatibility is often a strength, but with something that has changed so often over the time, things get confusing, especially as strings are such an essential datatype that one may assume that they should be simple