有一段文字,有中文字符也有英文字符,现在我要把它分成几段,每段不能超100个字,在程序中怎么分。我用了几种方法,总分出现乱码(string类弄存储的)。

解决方案 »

  1.   

    1 自己通过判断字符的最高位为1则为中文字符,然后自己计算
    2 用widestring,分好了再转换为string
      

  2.   

    请参照function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string编写自己的函数
      

  3.   

    procedure StringToMultiByte(s: string; var buf: array of word; bufstr: integer;
      size: integer);
    var
      pos, len: integer;
      low, high: word;
      num: integer;
    begin
      num := bufstr;
      pos := 1; // string start pos always is 1;
      len := length(s);
      while pos <= len do
      begin
        if num < (size - 1) then
        begin
          high := byte(s[pos]);
          if high > 128 then // &ordm;&ordm;×&Ouml;
          begin
            low := byte(s[pos + 1]);
            buf[num] := (high shl 8) + low;
            inc(pos, 2);
          end
          else
          begin // &Oacute;&cent;&Icirc;&Auml;
            buf[num] := high;
            inc(pos, 1);
          end;
          inc(num, 1);
        end
        else
        begin
          buf[num] := 0;
          break;
        end;
      end;
      buf[num] := 0;
    end;