在 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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM