Lazarus 中的字符串 String,AnsiString,UnicodeString,UTF8String,WideString

  Lazarus 在字符串处理上默认是UTF8编码

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

UTF8Decode(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        UTF8String(s) to            复件123  :  len=9
with        UTF8Decode(s) to              ??123  :  len=5
with        Utf8ToAnsi(s) to              ??123  :  len=7

强制后长度等于6 ,不明白啊!

posted @   海利鸟  阅读(3878)  评论(1编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
点击右上角即可分享
微信分享提示