delphi10 判断中文、英文、中文标点、英文标点、数字函数


function IsChinesePunct(const c: Char): Boolean;
var
charCode: Integer;
begin
charCode := Ord(c);
// 匹配中文标点的核心Unicode区间
Result := (charCode >= $3000) and (charCode <= $303F) // 全角符号区
or (charCode >= $FF00) and (charCode <= $FFEF) // 全角ASCII对应标点
or (charCode >= $4E00) and (charCode <= $9FFF); // 汉字附带标点(可选)
end;

function IsEnglishPunct(const c: Char): Boolean;
var
charCode: Integer;
begin
charCode := Ord(c);
// 第一步:先判断是否在ASCII可打印区间($0020-$007F)
Result := (charCode >= $20) and (charCode <= $7F);
if Result then
begin
// 第二步:排除英文字母(A-Z, a-z)和数字(0-9),剩余的就是英文标点
Result := not ((charCode >= $30) and (charCode <= $39)) // 排除0-9
and not ((charCode >= $41) and (charCode <= $5A)) // 排除A-Z
and not ((charCode >= $61) and (charCode <= $7A)); // 排除a-z
end;
end;

function IsChineseChar(const c: Char): Boolean;
begin
// 核心:判断字符的Unicode编码是否在中文核心区间[$4E00, $9FFF]
Result := (Ord(c) >= $4E00) and (Ord(c) <= $9FFF);
end;


function IsEnglishChar(const c: Char): Boolean;
begin
// 核心:判断字符的Unicode编码是否在大写字母或小写字母区间
Result := ((Ord(c) >= $0041) and (Ord(c) <= $005A)) // A-Z
or ((Ord(c) >= $0061) and (Ord(c) <= $007A)); // a-z
end;


function IsDigitByBuiltIn(const c: Char): Boolean;
begin
// 需要引入System.Character单元
// 直接调用内置函数,无需关心编码,更简洁
Result := IsDigit(c);
end;

调用:

procedure TForm1.btn2Click(Sender: TObject);
var
i: integer;
s: string;
inputChar: Char;
begin
inputChar := edt1.Text[1];

if IsChineseChar(inputChar) then
ShowMessage(s + ' 的第 ' + inttostr(i) + '个字符是中文')
else if IsEnglishChar(inputChar) then
ShowMessage(s + ' 的第 ' + inttostr(i) + '个字符是西文')
else if IsChinesePunct(inputChar) then
ShowMessage(s + ' 的第 ' + inttostr(i) + '个字符是中文标点')
else if IsEnglishPunct(inputChar) then
ShowMessage(s + ' 的第 ' + inttostr(i) + '个字符是英文标点')
else if IsDigitByBuiltIn(inputChar) then
ShowMessage(s + ' 的第 ' + inttostr(i) + '个字符是数字')
else
ShowMessage('None');

end;

 

posted @ 2026-01-30 17:02  绿水青山777  阅读(0)  评论(0)    收藏  举报