关闭

处理英文单词换行显示

1895人阅读 评论(0) 收藏 举报

     在smartforms实际使用中经常会遇到换行的情况,如果是中文,那还好办,如果是英文,就经常会出现英文单词分两行显示的情况,因此,我自己写了个处理function,能够纠正这种错误,如果要换行,之前会用空格补足位数。

FUNCTION zsplittext.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(FIXLENGTH) TYPE  I
*"  CHANGING
*"     REFERENCE(ORITEXT) TYPE  STRING
*"----------------------------------------------------------------------

*"AS,DFG HJKL,ASDFJ,SDKF22
*"----------------------------------------------------------------------

  DATA: tmp_text TYPE string,
        char,
        tmp_text1 TYPE string,
        tmp_sub_t TYPE string,
        sub_t TYPE string.
  DATA: textlength TYPE i,
        f_length TYPE i,
*        comm_n TYPE i,
        t_length1 TYPE i,"累加字符串长度
        t_length2 TYPE i,
        t_length3 TYPE i,
        t_rown TYPE i,"
        space_length TYPE i,
        sub_text_len TYPE i.
  DATA: dt_subt1 TYPE TABLE OF string,
        dt_subt2 TYPE TABLE OF string.
  TYPES: BEGIN OF s_subt,
        sub_text TYPE string,
        splitsign(5) TYPE c,
        END OF s_subt.
  DATA: dt_subtext TYPE TABLE OF s_subt WITH HEADER LINE.
  DATA: l_tabix LIKE sy-tabix.

  textlength = STRLEN( oritext ).
  IF textlength > fixlength.
*    comm_n = fixlength - 1.
*    tmp_text = oritext+comm_n(3).
    char = oritext+fixlength(1).
    IF char <> ' ' AND char <> ',' AND char <> '.'.
      SPLIT oritext AT ',' INTO TABLE dt_subt1."先用,分割
*      IF sy-subrc <> 0.
*        APPEND oritext TO dt_subt1.
*      ENDIF.
      LOOP AT dt_subt1 INTO sub_t.
        l_tabix = sy-tabix.
        tmp_sub_t = sub_t.
        CONDENSE tmp_sub_t NO-GAPS.
        IF STRLEN( tmp_sub_t ) < STRLEN( sub_t ).
          SPLIT sub_t AT space INTO TABLE dt_subt2."再用空格分割

          LOOP AT dt_subt2 INTO sub_t.
            dt_subtext-sub_text = sub_t.
            IF sy-tabix = 1.
              dt_subtext-splitsign = ','.
            ELSE.
              dt_subtext-splitsign = 'space'.
            ENDIF.
            APPEND dt_subtext.
          ENDLOOP.
        ELSE.
          dt_subtext-sub_text = sub_t.
          IF l_tabix <> 1.
            dt_subtext-splitsign = ','.
          ENDIF.
          APPEND dt_subtext.
        ENDIF.
      ENDLOOP.

      CLEAR tmp_text.
      LOOP AT dt_subtext.
        t_length1 = STRLEN( tmp_text ).
        t_length3 = t_length1 - t_rown * fixlength.
        sub_text_len = STRLEN( dt_subtext-sub_text ).
        IF t_length1 = 0.
          tmp_text = dt_subtext-sub_text.
        ELSE.
          IF t_length3 < fixlength.
            IF dt_subtext-splitsign = ','.
              CONCATENATE tmp_text dt_subtext-sub_text INTO tmp_text SEPARATED BY ','.
            ELSEIF dt_subtext-splitsign = 'space'.
              CONCATENATE tmp_text dt_subtext-sub_text INTO tmp_text SEPARATED BY space.
            ENDIF.
          ENDIF.

          t_length1 = STRLEN( tmp_text ).
          t_length3 = t_length1 - t_rown * fixlength.

          IF t_length3 = fixlength.
            IF dt_subtext-splitsign = ','.
              CONCATENATE tmp_text ',' INTO tmp_text.
              sub_text_len = sub_text_len + 1.
            ELSEIF dt_subtext-splitsign = 'space'.
              CONCATENATE tmp_text dt_subtext-sub_text INTO tmp_text SEPARATED BY space.
            ENDIF.
          ENDIF.

          t_length1 = STRLEN( tmp_text ).
          t_length3 = t_length1 - t_rown * fixlength.

          IF t_length3 > fixlength.
            t_rown = t_rown + 1.
            f_length = t_rown * fixlength - 1.
            tmp_text1 = tmp_text+f_length(1).

            IF tmp_text1 <> ' ' AND tmp_text1 <> ','.
*             计算需要几个空格来填充一行到末尾
              t_length2 = t_length1 - sub_text_len.
              space_length = fixlength - ( t_length3 - sub_text_len ).
*             先把最后加上的字符串去掉
              tmp_text = tmp_text+0(t_length2).
*             循环加空格
              DO space_length TIMES.
                CONCATENATE tmp_text '' INTO tmp_text SEPARATED BY space.
              ENDDO.
              CONCATENATE tmp_text dt_subtext-sub_text INTO tmp_text.
            ELSE.
              CONCATENATE tmp_text dt_subtext-sub_text INTO tmp_text SEPARATED BY space.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDLOOP.
      oritext = tmp_text.
    ENDIF.
  ENDIF.
ENDFUNCTION.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:45900次
    • 积分:513
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:16篇
    • 译文:3篇
    • 评论:0条
    文章分类