function ByteType(const S: string; Index: Integer): TMbcsByteType; begin Result := mbSingleByte; //默认为英文字符 if SysLocale.FarEast then //操作系统是亚洲版本 Result := ByteTypeTest(PChar(S), Index-1); end;function ByteTypeTest(P: PChar; Index: Integer): TMbcsByteType; var I: Integer; begin Result := mbSingleByte; if (P = nil) or (P[Index] = #$0) then Exit; if (Index = 0) then begin if P[0] in LeadBytes then Result := mbLeadByte; end else begin I := Index - 1; while (I >= 0) and (P[I] in LeadBytes) do Dec(I);// if ((Index - I) mod 2) = 0 then Result := mbTrailByte else if P[Index] in LeadBytes then Result := mbLeadByte; end; end;
根据ASC码来判断是否有汉字..
如果是则可以知道当前字节和它后面那个字节起码不是英文
begin
Result := mbSingleByte; //默认为英文字符
if SysLocale.FarEast then //操作系统是亚洲版本
Result := ByteTypeTest(PChar(S), Index-1);
end;function ByteTypeTest(P: PChar; Index: Integer): TMbcsByteType;
var
I: Integer;
begin
Result := mbSingleByte;
if (P = nil) or (P[Index] = #$0) then Exit;
if (Index = 0) then
begin
if P[0] in LeadBytes then Result := mbLeadByte;
end
else
begin
I := Index - 1;
while (I >= 0) and (P[I] in LeadBytes) do Dec(I);//
if ((Index - I) mod 2) = 0 then Result := mbTrailByte
else if P[Index] in LeadBytes then Result := mbLeadByte;
end;
end;