Lazarusの文字列String,AnsiString,UnicodeString,UTF 8 String,WideString

4028 ワード

Lazarusの文字列処理のデフォルトはUTF 8エンコーディングです
UTF8String          = type ansistring;

次のコードのように
s:=trim('abcd123');

  lbStrLen1.Caption:=IntToStr(Length(s));   { UTF8 }

  lbStrLen2.Caption:=IntToStr(length(Utf8ToAnsi(s)));

  lbStrLen3.Caption:=IntToStr(Length(UTF8Decode(s)));

出力7,7,7
それともそのコード
s:=trim(' 123');

  lbStrLen1.Caption:=IntToStr(Length(s));   { UTF8 }

  lbStrLen2.Caption:=IntToStr(length(Utf8ToAnsi(s)));

  lbStrLen3.Caption:=IntToStr(Length(UTF8Decode(s)));

結果は9,7,5
UTF 8 Decode(s)復号はUnicodeStringである.
っていうか、ここまで強制しちゃダメなんだよ!不定コンパイラはスマートです.
s:=' 123';

  Memo3.Append(format('with %20s to %20s  :  len=%d',['AnsiString(s)',s,length(s)]));

  Memo3.Append(format('with %20s to %20s  :  len=%d',['UnicodeString(s)',UnicodeString(s),length(UnicodeString(s))]));

  Memo3.Append(format('with %20s to %20s  :  len=%d',['WideString(s)',WideString(s),length(WideString(s))]));

  Memo3.Append(format('with %20s to %20s  :  len=%d',['UTF8String(s)',UTF8String(s),length(UTF8String(s))]));

  Memo3.Append(format('with %20s to %20s  :  len=%d',['UTF8Decode(s)',UTF8Decode(s),length(UTF8Decode(s))]));

  Memo3.Append(format('with %20s to %20s  :  len=%d',['Utf8ToAnsi(s)',Utf8ToAnsi(s),length(Utf8ToAnsi(s))]));

結果は
with AnsiString(s)to複件123:len=9 with UnicodeString(s)to複件123:len=6 with WideString(s)to複件123:len=6 with UTF 8 String(s)to複件123:len=9 with UTF 8 Decode(s)to??123  :  len=5 with        Utf8ToAnsi(s) to              ??123  :  len=7
強制後の長さは6に等しくて、分かりません!