在Delphi中的中英文混合的字符串中,怎样判断某个字符串变量中是否有汉字?

解决方案 »

  1.   


    while j<= strlen(pchar(字串)) do
     if ord(字串[j])>128 then
    showmessage('是汉字');
      

  2.   


    while j<= strlen(pchar(字串)) do
    begin
     if ord(字串[j])>128 then
    begin
     showmessage('是汉字');
     inc(j,2);
    end
    else
    inc(j,1);
    end;
      

  3.   

    汉字的第一个字符是大于128的,英文的是小于128的
    你这样写好了
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if ord(key)<128 then
        key:=#0;
    end;var
      source: String;
      chinese: String;
      English: String;
      len: Integer;
      i: integer;
    begin
      source := '日本pig';
      english := '';
      chinese := '';
      len := Length(source);
      for i := 1 to len do
      begin
        if Ord(source[i]) > 128 then
          chinese := chinese + source[i]
        else
          english := english + source[i];
      end;
      ShowMessage(chinese);
      ShowMessage(english);
    end;
      

  4.   

    能否这样?if length(str)<>length(widestring(str)) then
    showmessage('this string has Chinese character');
      

  5.   

    楼上兄弟的方法偶没试过,感觉有可能有问题的~~
    如果开始定义str的时候是string类型的,那么默认AnsiString 类型,这样的话,强制转换WideString(str)就对长度没影响,不会合并的,但是如果定义str的时候是定义WideString类型的,那么就会发生长度的变化~~~
    偶是新人,^_^~~~~~
      

  6.   

    咯嚓,匆忙中try了一把,好像还可以的啊,^_^,明天再来解决·~~~~
      

  7.   

    delphi帮助是这样说的
    For single-byte (AnsiString) and multibyte strings, Length returns the number of bytes used by the string. For Unicode (WideString) strings, Length returns the number of bytes divided by two.对于AnsiString字符串,length返回的是字符串的字节数,而对于Unicode的字符串,则返回的是字节数的一般,所以length(widestring(str))就返回了str的实际字数