[fpc-pascal] Remove last character from pchar

Jean SUZINEAU jean.suzineau at wanadoo.fr
Fri Jun 11 00:49:37 CEST 2021


Note: if your string is UTF8 encoded, the last "character" can be 
encoded, can span over 1 to 4 "Char", you need to know the length of 
your character.

For example:

program Project1;
{$mode objfpc}{$H+}

uses
   SysUtils, Classes;
var
    s: array[0..30] of char;
    p: PChar;
    i: Integer;
begin
      p:= @s;
      StrPLCopy( p, 'Test€a', 30);
      Writeln( p);
      for i:= 0 to 7
      do
        Write( ord(s[i]),':',s[i],' ');
      Writeln;
      s[5]:= #0;
      Writeln( p);
      for i:= 0 to 7
      do
        Write( ord(s[i]),':',s[i],' ');
      Writeln;
end.

gives

$ ./project1
Test€a
84:T 101:e 115:s 116:t 226:� 130:� 172:� 97:a
Test�
84:T 101:e 115:s 116:t 226:� 0: 172:� 97:a
$

(the € character which spans over 3 chars is broken and so the end of 
the string becomes unreadable)



More information about the fpc-pascal mailing list