Menu

#16 RightStr doesn't work

open
nobody
None
5
2007-04-05
2007-04-05
Anonymous
No

In the attached example there is a problem reading a UTF file text.
It seemes that RightStr and MidStr functions don't work when the file has at the beginning the Byte Order Mark.

Without uses utf8vcl there isn't this problem, but of course the strings aren't well displayed

With Tnt components, using WideString instead of String variables and without utf8vcl everything works fine).

Discussion

  • Nobody/Anonymous

     
  • Dennis Rodman

    Dennis Rodman - 2007-04-05

    Logged In: YES
    user_id=1761940
    Originator: NO

    The same problem using consts instead of opening a file. It seems something between RightStr (or MidStr) and the BOM sequence.
    Look the follow new version example:

    procedure TForm1.FormCreate(Sender: TObject);
    const
    TestText1_BOM = #$EF#$BB#$BF#$52#$6F#$77#$20#$6E#$75#$6D#$62#$65#$72#$20#$31#$20#$3A#$20#$D1#$84#$D0#$B8#$D1#$81#$D0#$B2#$D1#$83#$D0#$B0#$D0#$BF#$D1#$80#$D1#$88#$0D#$0A;
    TestText1 = #$52#$6F#$77#$20#$6E#$75#$6D#$62#$65#$72#$20#$31#$20#$3A#$20#$D1#$84#$D0#$B8#$D1#$81#$D0#$B2#$D1#$83#$D0#$B0#$D0#$BF#$D1#$80#$D1#$88#$0D#$0A;
    TestText2 = #$52#$6F#$77#$20#$6E#$75#$6D#$62#$65#$72#$20#$32#$20#$3A#$20#$D1#$84#$D0#$B8#$D1#$81#$D0#$B2#$D1#$83#$D0#$B0#$D0#$BF#$D1#$80#$D1#$88#$0D#$0A;
    TestText3 = #$52#$6F#$77#$20#$6E#$75#$6D#$62#$65#$72#$20#$32#$20#$3A#$20#$D1#$84#$D0#$B8#$D1#$81#$D0#$B2#$D1#$83#$D0#$B0#$D0#$BF#$D1#$80#$D1#$88#$0D#$0A;
    var
    s, s1: string;
    BOM: string[3]; //Byte Order Mark
    l: integer;
    begin
    //first line with eventual BOM:
    s := TestText1_BOM; //s := TestText1_BOM
    BOM := LeftStr(s, 3);
    if (ord(BOM[1]) = $EF) and (ord(BOM[2]) = $BB) and (ord(BOM[3]) = $BF) then
    begin
    l := Length(s);
    s1 := RightStr(s, l - 3); //s1 := MidStr(s, 4, l - 3);
    Memo1.Lines.Add(s1);
    end
    else
    Memo1.Lines.Add(TestText1);

    //others lines:
    s := TestText2;
    Memo1.Lines.Add(s);
    s := TestText3;
    Memo1.Lines.Add(s);
    end;

     

Log in to post a comment.

MongoDB Logo MongoDB