如题......

解决方案 »

  1.   

    {=============================================================================}function newline(str: string; width: integer): string;
    var
      buf, resultBuf, estr: wideString;
      str1, str2, cstr: string;
      cnum, enum, i, j, mnum: integer;
    label resultlab, numlab;
    begin
      //转成宽字符后可以不用判断汉字
      buf := str;
      resultBuf :=''; //初始化结果
      while Length(buf) > width do
      begin
        estr := copy(buf, 1, width);
        Delete(buf, 1, width);
        ///////////////////////////如有数字,则从后面截取字////////////////////////////////////
        mnum := 0;
        cstr := estr;
        cnum := length(cstr) - length(estr);
        enum := 2 * length(estr) - length(cstr);
       { if trunc(cnum + enum / 2) <= width then
        begin
          if enum mod 2 = 1 then
          begin
          /////////////////////空格不能插入数字中///////////////////////////////////
            str1 := '';
            str1 := Copy(estr, Length(estr) - mnum, 1);
            while (((str1 >= '0') and (str1 <= '9')) or (str1 = '.')) do
            begin
              mnum := mnum + 1;
              str1 := Copy(estr, Length(estr) - mnum, 1);
            end;
            enum := enum + 1;
            insert(' ', estr, length(estr) - mnum)
            //estr := estr + ' ';
          end;   }
        if trunc(cnum + enum / 2) < width then
        begin
          numlab: if trunc(enum / 2) < length(buf) then
          begin
            estr := estr + copy(buf, 1, (width - trunc(cnum + enum / 2)));
            Delete(buf, 1, (width - trunc(cnum + enum / 2)));
          end
          else
          begin
            estr := estr + copy(buf, 1, length(buf));
            Delete(buf, 1, length(buf));
            goto resultlab;
          end;    end;
      //end;
         /////////////////////处理数字///////////////////////
        str1 := '';
        str2 := '';
        mnum := 0;
        str1 := Copy(estr, Length(estr), 1);
        str2 := copy(buf, 1, 1);
        cstr := estr;
        cnum := length(cstr) - length(estr);
        enum := 2 * length(estr) - length(cstr);
        while (((str1 >= '0') and (str1 <= '9')) or (str1 = '.'))
          and (((str2 >= '0') and (str2 <= '9')) or (str2 = '.')) do
        begin
          mnum := mnum + 1;
          estr := estr + str2;
          delete(buf, 1, 1);
          str1 := '';
          str2 := '';
          str1 := Copy(estr, Length(estr), 1);
          str2 := copy(buf, 1, 1);
          //showmessage('str1:'+str1+'  str2:'+str2);
        end;
        //showmessage('mnum'+inttostr(mnum)+' width:'+inttostr(width)+' cnum: '+inttostr(cnum)+' enum: '+inttostr(trunc(enum / 2)));
        if mnum > (width - cnum - trunc(enum / 2)) + 1 then
        begin
          str1 := '';
          str1 := Copy(estr, Length(estr), 1);
          while (((str1 >= '0') and (str1 <= '9')) or (str1 = '.')) do
          begin
            buf := copy(estr, Length(estr), 1) + buf;
            estr := copy(estr, 1, Length(estr) - 1);
            str1 := '';
            str1 := Copy(estr, Length(estr), 1);
          end;
          goto resultlab;
        end
        else
          if mnum < (width - cnum - trunc(enum / 2)) + 1 then
            if mnum > (width - cnum - trunc(enum / 2)) then
            begin
              estr := estr + copy(buf, 1, mnum);
          //showmessage(estr) ;
              delete(buf, 1, mnum);
            end;
        ///////////////////////
        cstr := estr;
        cnum := length(cstr) - length(estr);
        enum := 2 * length(estr) - length(cstr);
        if trunc(cnum + enum / 2) < width then
          goto numlab;
        //////////////////////////
        {/////处理标点符号////////////////////////////////////// }
        str1 := '';
        str1 := copy(buf, 1, 1);
        if (str1 = ',') or (str1 = ',') or (str1 = '。') or (str1 = '.') or (str1 = '?') or (str1 = '?')
          or (str1 = '!') or (str1 = '!') or (str1 = ';') or (str1 = ';') or (str1 = '、') or (str1 = '》')
          or (str1 = '”') or (str1 = '"') or (str1 = ')') or (str1 = ')') then
        begin
          estr := Copy(estr, 1, Length(estr)) + str1;
          delete(buf, 1, 1);
        end;
       { /////////////////////加空格////////////////////////////////////////// }
        {resultlab: cstr := estr;
        cnum := length(cstr) - length(estr); //汉字个数
        enum := 2 * length(estr) - length(cstr); //数字个数
        if trunc(cnum + enum / 2) < width then
        begin
          j := 6;
          for i := 1 to (width - trunc(cnum + enum / 2)) do
          begin
          //////////////////空格不能插入数字中////////////////////////
            str1 := '';
            str1 := copy(estr, j - 1, 1);
            while (j = 1) or ((str1 >= '0') and (str1 <= '9')) or (str2 = '.') do //判断是否数字
            begin
              j := j + 2;
              str1 := '';
              str1 := copy(estr, j - 1, 1);
            end;
            insert(' ', estr, j);
            j := j + 2;
          end;
        end;}
        resultlab:
        if buf = '' then
          resultBuf := resultBuf + estr
        else
          resultBuf := resultBuf + estr + #13#10;
      end;
      resultBuf := resultBuf + buf;
      Result := resultBuf;
    end;{=============================================================================}