zoukankan      html  css  js  c++  java
  • 在 Delphi 中判断一个字符是中文的方法

    参考这个博客

    https://www.cnblogs.com/rogge7/p/6118588.html 

    function IsMBCSChar(const ch: AnsiString): Boolean; //转载时修改
    begin 
      Result := (ByteType(ch, 1) <> mbSingleByte); 
    end; 

    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;

    ---------------------
    作者:sunylat
    来源:CSDN
    原文:https://blog.csdn.net/sunylat/article/details/30723289
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    2.4模拟赛
    2.3模拟赛
    初入博客园
    [SHOI2017]期末考试
    [整理]svn常见问题汇总
    转:Cookies 和 Session的区别
    常看的几个网站:推荐给大家
    电脑硬件基础知识
    offsetLeft和style.left的区别
    用Javascript实现图片的缓慢缩放效果
  • 原文地址:https://www.cnblogs.com/westsoft/p/10220096.html
Copyright © 2011-2022 走看看