• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//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;

来源:http://www.delphitop.com/html/zifuchuan/2877.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap