请问如何判断一个字符串末尾一个字符是否是半个汉字,并把它去掉??或者说是如何判断一个字符串末尾一个字符是否正常!?!?

解决方案 »

  1.   

    好象是function byte():boolean;用byte查HELP。
      

  2.   

    var
      S:String;
    begin
      if Ord(RightStr(S,1)[1])>128 then
        S:=LeftStr(S,Length(S)-1);
    end;
      

  3.   

    那何种字符属于不正常?按照你的意思应该是ASCII码在127以上的都不正常吧,那你直接用Ord取值,>127就删了
      

  4.   

    function ByteType(const S: string; Index: Integer): TMbcsByteType;DescriptionCall ByteType to determine the type of the byte specified by the Index parameter, where 0 specifies the first byte in S, 1 specifies the second byte, and so on.If the system is not using a multi-byte character system (MBCS), ByteType always returns mbSingleByte. Otherwise, ByteType returns mbSingleByte if the indicated byte represents a complete character in S, mbLeadByte if it represents the first byte of a double byte character, and mbTrailByte if it represents the second byte of a double byte character.这个函数好用。返回0或1。0代表汉字的第一个字节,1代表汉字的第二个字节。
      

  5.   

    这没有本质区别,还是上面的意思,二进制数10000000就是128,也就是说最高位为1就是大于127,也就是说在7位ASCII码范围内的字符才是合法的.
      

  6.   

    "每个字节的最高位相同"也很好理解,为了与系统中基本的ASCII字符集区分开,汉字编码是用ASCII扩展字符集中的字符实现,自然最高位都是1.
      

  7.   

    如何截剪中文字串,而不会出现乱码
    --------------------------------------------------------------------------------   [INLINE]  如何截剪中文字串,而不会出现乱码Q:
       各位先进!!
           我尝试将一个长字串,内含中文字及半形字,截剪成固定长度的字串如何能
           避免截剪到中文字,而出现乱码?A:
           [INLINE]
           您可以参考本信举出的例子, 重点在於切出来的字串, 如何判定其最後一个
           字元是否是一个中文字的前半字.
           我采用的是从头检视到最後的演算法, 也许有人会怀疑 --
           [INLINE] 1. 为什麽不直接抓最後一个字元判断? 因为中文字的
           Trail-byte, 其内码也可能落在 Lead-byte 的内码区间内.
           [INLINE] 2. 为什麽不直接抓最後两个字元来判断? 因为前一个字的
           Trail-byte 加上後一个字的 Lead-byte, 可能又是一个中文字.
           以上的考虑是基於DelphiChat以前讨论过「判断一个字是否为 BIG-5 中文字
           ?」的讨论, 判断时的采用的方法与可能的问题我在此就不重覆了, 请大家
           自行参阅.
           如果是 Win3.1, 11/25 robin <[email protected]>曾发表笔划计算函数,
           其中的中文字判断不是用 IsDBCSLeadByte, 而是检查内码区间, 也一并建议
           给您参考. (下载 Robin 的笔划计算函数)
           [INLINE] 1. 在 form 上放置两个 TMemo 元件, 一个 TButton
           [INLINE] 2. Button1 的 OnClick 事件.
    procedure TForm1.Button1Click(Sender: TObject);
    const
      _LengthPerLine = 6;
    var
      i: integer;
      sLine: string;
      sCuted: string;
      iCutLength: integer;
      j: integer;
      bIsDBCS: boolean;
    begin
      if _LengthPerLine < 2 then Exit;
      Memo2.Lines.Clear;  for i := 0 to Memo1.Lines.Count - 1 do
      begin
        sLine := Memo1.Lines[i];
        if Length(sLine) = 0 then // 如果是空行的话
          Memo2.Lines.Add(#13+#10)
        else
        repeat
          // 截出 _LengthPerLine 个字元出来
          iCutLength := _LengthPerLine;
          sCuted := Copy(sLine, 1, iCutLength);
          iCutLength := Length(sCuted);
          bIsDBCS := False;
          // 看看最後一个字元是不是中文的前半个字
          for j := 1 to iCutLength do
          begin
            if bIsDBCS then
              bIsDBCS := False
            else
              if Windows.IsDBCSLeadByte(byte(sCuted[j])) then
                bIsDBCS := True;
          end;
          // 如果最後一个字是中文的话, 少截一个字元
          if bIsDBCS then Dec(iCutLength);
          // 截出确定长度的字元并加入 Memo2 中
          Memo2.Lines.Add(Copy(sLine, 1, iCutLength));
          // 从 sLine 中去掉已送至 Memo2 的文字.
          sLine := Copy(sLine, iCutLength + 1, Length(sLine) - iCutLength);
        until Length(sLine) <= 0;
      end;
    end; 
      

  8.   

    var
      TheWideChar:wideString;
      TheChar:String;
      FirstHalfChar,Secondhalfchar:String;
    begin
      //先把你需要判断的字符取到变量TheWideChar中,类型WideString用来防止取到半个字符
      TheChar:=TheWidechar;
      FirstHalfChar:=TheChar[1];//将前半个字节给变量FirstHalfChar
      SecondHalfChar:=TheChar[2]; 
      if ord(FirsthalfChar[1])>=161 then  //因为汉字编码第一个字节从A1,也就是十进制的161开始,第二个字节也是从A1开始编码的(我指的是GB2312字符集,而GBK或Big5等不是这个编码范围)
      begin
        showmessage('汉字');
      end
      else
      begin
        showmessage('不是汉字');
      end;
    end;
      

  9.   

    使用这几个函数
    ismbbtrail
    ismbblead
    ismbcl0
    ismbcl1
    ismbcl2