稀里糊涂混成了个猩猩,散分以感谢帮助过我的网友们,谢谢你们啦!!!
另,我有一问题,有字符串包含有汉字、英文和数字,比如:关于某甲公司欠TCL北京分公司200万元的问题..... 现在需要把它转变成一个定宽的字符串列表,假如宽度定为14个汉字,则上面的字符串变为:关于某甲公司欠TCL北京分公司2
00万元的问题.....我用了以下方法:
...
Copy(WideString(S),1,14);
...
其结果为:关于某甲公司欠TCL北京分公
司200万元的问题.....不符合我的要求,请问各位如何解决。
谢谢先!

解决方案 »

  1.   

    ansi,与汉字的宽度不同,我也有这样的问题,麻烦,帮你顶
      

  2.   

    WideString(S) 此函数转换后会把所有英文字母及数字转换成与汉字一样的宽度,所以才会出现你目前所看到的结果.
      

  3.   

    依次检测Widestring中的字符值,汉字高低字节都有值,字母高字节为0。如:关:$5173,T:$0054
      

  4.   

    我常用的代码:function Cut(St : String; W : integer) : String;
    var
      i : integer;
      W1, Cur : integer;
    begin
      if W <= 1 then W := 1;
      W := W * 2; // 每个汉字占两个字符
      i := 1;
      Result := '';
      Cur := 0;
      while i <= Length(St) do
      begin
        if St[i] >= #128  // 是汉字或其它占两个字节的字符
          then W1 := 2 else W1 := 1;
        if Cur + W1 > W then
        begin
          Result := Result + #13#10; //换行
          Cur := 0;
        end;
        inc(Cur, W1);
        Result := Result + St[i];
        if (W1 = 2) and (i < Length(St)) then Result := Result + St[i + 1];
        inc(i, W1);
      end;
    end;调用cut('关于某甲公司欠TCL北京分公司200万元的问题.....', 14),返回:关于某甲公司欠TCL北京分公司20
    0万元的问题.....
      

  5.   

    改成 Copy(AnsiString(S), 1, 28);
      

  6.   

    jellypillar(新型人类)的代码好用
      

  7.   

    问题搞掂。多谢以上各位啦!尤其是 jellypillar(新型人类)。我最后的代码为:
    procedure StrToList(const S: string; Width: integer; var OutList: TStringList);
    var
      i, W1, Cur : integer;
      TmpStr, Str : string;
    begin
      if Width <= 1 then Width := 1;
      Width := Width * 2; // 每个汉字占两个字符
      i := 1;
      Cur := 0;
      Str := S;
      OutList.Clear;
      TmpStr := '';
      while i <= Length(Str) do
      begin
        if Str[i] >= #128  // 是汉字或其它占两个字节的字符
          then W1 := 2 else W1 := 1;
        if Cur + W1 > Width then
        begin
          OutList.Add(TmpStr);
          TmpStr := '';
          Cur := 0;
        end;
        inc(Cur, W1);
        TmpStr := TmpStr + Str[i];
        if (W1 = 2) and (i < Length(Str)) then
           TmpStr := TmpStr + Str[i + 1];
        inc(i, W1);
      end;
      OutList.Add(TmpStr);
    end;使用时采用一下形式:
    ....
    try
      SS := TStringList.Create;
      StrToList(Edit1.Text, 10, SS);
      ....
    finally
      SS.Free;
    end;
    ....复杂过jellypillar(新型人类),但方便我后续处理。
      

  8.   

    其实我也想过用TStrings,不过想到直接用String会简单一些,呵呵,
    一点建议:OutList用TStrings而不用TStringList,应用的范围会更广一些。但SS := TStringList.Create那句不能改。:)
      

  9.   

    记得刚参加工作,经理就让我做这样的函数,要是自己写的话必须考虑半个汉字问题,全角/半角问题,行尾括号,引号,单引号,双引号等问题麻烦得很,还好Borland给咱提供了一个现成的函数,我只用试试看的心理试用了一下,咦!果然很好用! 看这里,看这里,嘻嘻...:SysUtils.pas{ WrapText will scan a string for BreakChars and insert the BreakStr at the
      last BreakChar position before MaxCol.  Will not insert a break into an
      embedded quoted string (both ''' and '"' supported) }function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet;
      MaxCol: Integer): string; overload;
    function WrapText(const Line: string; MaxCol: Integer = 45): string; overload;
      

  10.   

    先转换成Unicode再计算字符串长度就可以了