Delphi判断字符串中是否包含汉字,并返回汉字位置


本文整理自网络,侵删。

 //1,函数代码
{
判断字符串是否包含汉字
// judgeStr:要判断的字符串
//posInt:第一个汉字位置
}
function TForm2.IsHaveChinese(judgeStr: string; var posInt: integer): boolean;
var
  p: PWideChar; // 要判断的字符
  count: integer; // 包含汉字位置
  isHave: boolean; // 是否包含汉字返回值
begin
   isHave := false; // 是否包含汉字返回值默认为false
   count := 1; // 包含汉字位置默认为1
   p := PWideChar(judgeStr); // 把要判断字符串转换

// 循环判断每个字符
while p^ <> #0 do
begin
   case p^ of
   #$4E00 .. #$9FA5:
     begin
       isHave := true; // 设置是否包含汉字返回值为true
       posInt := count; // 设置包含汉字位置
       break; // 退出循环
     end;
   end;
  Inc(p);
  Inc(count); // 包含汉字位置递增
end;
result := isHave;
end;


//2,例子:

procedure TForm2.Button3Click(Sender: TObject);
var
  testStr1, testStr2: string;
  posInt: integer;
begin
  testStr1 := '12345';
  testStr2 := '123汉字45';

if self.IsHaveChinese(testStr1, posInt) = true then
begin
  ShowMessage(testStr1 + ' 包含汉字 :' + inttostr(posInt));
end
else
begin
  ShowMessage(testStr1 + ' 不包含汉字');
end;

if self.IsHaveChinese(testStr2, posInt) = true then
begin
  ShowMessage(testStr2 + ' 包含汉字 :' + inttostr(posInt));
end
else
begin
  ShowMessage(testStr2 + ' 不包含汉字');
end;
end;

相关阅读 >>

Delphi 实现卸载windows应用程序(类似360软件管家-卸载程序)

Delphi 最简单的ttcpserver与ttcpclient通信实例

Delphi xe7 判断android应用程序中的通信状态类型

Delphi旋转位图

Delphi的tservice 服务路径获取 dll中获取文件路径

Delphi 串口 字符串转换ascii

Delphi禁用、启用usb

Delphi单元文件基本结构

indy tidtcpclient 在网络掉线时的处理方法

winapi 字符及字符串函数(14): chartooem、oemtochar

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...