首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么判断一个文件中含有汉字

2012-03-19 
如何判断一个文件中含有汉字?当然,这是一个文本文件,但是不知道他是GB2312、GBK还是UTF-8或UTF16编码的,可

如何判断一个文件中含有汉字?
当然,这是一个文本文件,但是不知道他是GB2312、GBK还是UTF-8或UTF16编码的,可能知道这个文件是否包含汉字吗?

[解决办法]
可以,把该文件的字符全转为ACS码
根据ASC码来判断是否有汉字..
[解决办法]
去看看编码规范
[解决办法]
用IsDBCSLeadByte检测是否是双字节的第一个字节,
如果是则可以知道当前字节和它后面那个字节起码不是英文
[解决办法]
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;

[解决办法]
其实很简单,把它转换成WideString,比较下长度是否和原来相等即可。
[解决办法]
呵呵,这个东西,主要是要看编码规范.谁有这些方面的详细资料就好了.

热点排行