ESubstringEncodingError Exception

Exception→ESubstringError→ESubstringEncodingError

type ESubstringEncodingError = class(ESubstringError);

An AnsiString, UTF8String, RawByteString, ShortString, AnsiChar or PAnsiChar argument.

Substring reads UTF-16 only. Delphi would convert such an argument to a string at a cost the call does not show, so Substring refuses it; convert it explicitly, string(a). A one-character literal from #$80 to #$FF is an AnsiChar to the compiler: Substring(#$E9) raises, Substring(Char(#$E9)) does not.

Examples

An AnsiString argument raises; converted, it is read.

delphi
uses Substr;

procedure Example;
var a: AnsiString;
    s: Substring;
    Raised: Boolean;
begin
    a := 'abc';
    s := Substring('xabc');
    Raised := False;
    try
        Assert(s.Contains(a));
    except
        on ESubstringEncodingError do Raised := True;
    end;
    Assert(Raised);
    Assert(s.Contains(string(a)));
end;