加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:00:03 所属栏目:大数据 来源:网络整理
导读://1,函数代码 { ? 判断字符串是否包含汉字 ? // judgeStr:要判断的字符串 ? //posInt:第一个汉字位置 } function TForm2.IsHaveChinese(judgeStr: string; var posInt: integer): boolean; var ? p: PWideChar; // 要判断的字符 ? count: integer; // 包

//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.cnblogs.com/del/archive/2009/04/14/1435722.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读