You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FreePascal中Substring()调用报错,求Unicode字符串截取方案

解决FreePascal中Unicode字符串截取的编译错误

编译错误原因

你遇到的Error: Illegal qualified是因为FreePascal中,即使启用了-MDelphiUnicode选项,UnicodeString也不是面向对象类型,不能通过.操作符调用Substring方法——这种调用方式是Delphi字符串对象的用法,但FreePascal的字符串是值类型,不支持该语法。

解决方案一:使用FreePascal内置Copy函数(推荐)

Pascal标准库提供的Copy函数是字符串截取的通用方式,适配所有Pascal环境,包括启用-MDelphiUnicode的场景。

修正后的代码

Function TChunk.Chunk(Constref input: String; location: TLocation): String;
Begin
  Result := Copy(input, location.FStart, location.GetLen);
End;

参数说明

  • Copy(S, StartPos, Length)的三个参数:
    1. S:待截取的原字符串
    2. StartPos:截取的起始位置(Pascal字符串索引从1开始,如果你的location.FStart是0-based的,需要改为location.FStart + 1)
    3. Length:要截取的字符长度,直接对应location.GetLen的返回值

解决方案二:使用SysUtils单元的Substring函数

如果你更习惯Delphi风格的命名,可以使用SysUtils单元提供的独立Substring函数(注意不是字符串的成员方法),需要先在所在单元的uses列表中添加SysUtils。

修正后的代码

// 确保所在单元的uses部分包含SysUtils
Function TChunk.Chunk(Constref input: String; location: TLocation): String;
Begin
  Result := Substring(input, location.FStart, location.GetLen);
End;

参数说明

  • Substring(S, StartPos, Length)的参数规则和Copy完全一致,同样需要注意起始位置为1-based索引。

内容的提问来源于stack exchange,提问作者RandomB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 09:45:41