我准备用copy函数复制一个字符串的一部分,如下:
strN:=copy(str,80,140)
我发现随着str的不同有时候返回的是乱码,有时候是一个字符串。
我怀疑是不是因为str是一个汉字字符串,所以才出现这个问题,
如果是这个原因引起的,您有什么办法能让返回的都是正常的字符串吗?

解决方案 »

  1.   

    演示程序中主要是用了 IsDBCSLeadByte 这个 API 来判断某字节是否在双字节字符集(例如汉字)的前导字节集中(GB 2312-80 汉字编码中的第一个字节范围 0xA1-0xFe) ( The IsDBCSLeadByte function determines whether a character is a lead byte ?that is, the first byte  of a character in a double-byte character set (DBCS).  ) procedure TForm1.Button1Click(Sender: TObject);var  CutLengthOfLine{ 被处理字符串的总长度 }, i, j: integer;  sLine{ 被处理的源字符串 }: string;  sCuted{ 按固定长度分割出来的部分字符串 }: string;  iCutLength{ 按固定长度分割出来的部分字符串的长度 }: integer;  bIsDBCS{ 是否是汉字的前半字节 }: boolean;beginif edit1.text='' then beginexit;end;  CutLengthOfLine:=strtoint(edit1.text);  if CutLengthOfLine < 2 then begin  showmessage('CutLengthOfLine 必须大于等于 2 !');  Exit;  end;  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 //开始处理字符串      iCutLength := CutLengthOfLine;      sCuted := Copy(sLine, 1, iCutLength);//从头取出 iCutLength 长的字符串      bIsDBCS := False;//先假设没有半个字符串      for j := 1 to iCutLength do //从头到尾逐个检查,至于为什么?//原作者是这样解释的//1. 为什麽不直接抓最後一个字元判断? 因为中文字的 Trail-byte, 其内码也可能落在 Lead-byte//   的内码区间内.//2. 为什麽不直接抓最後两个字元来判断? 因为前一个字的 Trail-byte 加上後一个字的 Lead-byte,//   可能又是一个中文字.      begin        if bIsDBCS then  //如果上一个字节是汉字的前半部分          bIsDBCS := False //则此时本字节是汉字的后半部分,          //所以将是否前半个汉字检测标志设为假        else          if Windows.IsDBCSLeadByte(byte(sCuted[j])) then            bIsDBCS := True;//否则检查本字节,并根据结果设置标志      end;  //end of for//如果最后一个字节的上一个字节是汉字的前半部分,则结束时//检测标志为假,      if bIsDBCS then Dec(iCutLength);//如果最后一个字节是汉字的前半部分, 则少截取一个字符,避免乱码      Memo2.Lines.Add(Copy(sLine, 1, iCutLength));      sLine := Copy(sLine, iCutLength + 1, Length(sLine) - iCutLength);//拷贝出下一部分固定长度的字符串,循环处理    until Length(sLine) <= 0;  end;memo2.setfocus;memo2.selstart:=0;memo2.SelLength:=0;end;
      

  2.   

    先转成 WideString 再 Copy .
      

  3.   

    试了一下,强转还会出现乱码
    直接把str定义为WideString
      

  4.   

    使用AnsiMidStr(str,start,len);String 和 WideString都OK.
      

  5.   

    var
      nString: String;
      iStart, iLength: Integer;
    begin
      nString := Edit2.Text;
      iStart := 1;
      iLength := 4;
      if Windows.IsDBCSLeadByte(Byte(nString[iStart + iLength])) then
        Edit3.Text := Copy(nString, iStart, iLength - 1)
      else
        Edit3.Text := Copy(nString, iStart, iLength)
    end;
      

  6.   

    我也有过类似的问题:
    你看看会有帮助的:
    procedure TRollCaption.SetValue;
    var i: Integer;
        j,num:integer;
        s:string;
    begin
      i:= 0;
      with ado do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select top '+intToStr(iNum)+'AL_ID, AL_Title from ArticleList order by Al_ID DESC');
        Open;
        while Not Eof do
        begin
          str[i].Info:= FieldByName('AL_Title').AsString;
          s:= FieldByName('AL_Title').AsString;
          if Length(str[i].Info) > 36 then
          begin
             num:=0;
             for j:=1 to 36 do
             begin
                if ord(s[j]) >128 then
                   num:=num+1;
             end;
             if (num mod 2) =0 then
               lab[i].Caption:= Copy(s,1,36)+'...'
             else
               lab[i].Caption:= Copy(s,1,35)+'...';
          end
          else
            lab[i].Caption:= str[i].Info;
          str[i].Link:= FieldByName('AL_ID').AsString;
          lab[i].Hint:= str[i].Link;
          inc(i);
          Next;
        end;
      end;
    end;