Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Divide text string

Former Member
0 Likes
1,095

Hi,

I need to divide a text string in parts of a maximum of 79 chars, and can't cut words in two. Is there a function or something to do it?

Thanks!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
950

hi,

check this thread

hope it will help you.

thanks

sachin

6 REPLIES 6
Read only

Former Member
0 Likes
950

is it that u need to only display any string in parts of 79 chars?

Read only

Former Member
0 Likes
951

hi,

check this thread

hope it will help you.

thanks

sachin

Read only

Former Member
0 Likes
950

Try this:

TYPES: BEGIN OF ty_text,

line(79) TYPE c,

END OF ty_text.

DATA: v_text TYPE string,

v_text1 TYPE string,

v_count TYPE p DECIMALS 2,

v_length TYPE i,

v_after TYPE i,

v_sub TYPE i,

it_text TYPE STANDARD TABLE OF ty_text,

wa_text TYPE ty_text.

v_text = 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh'. " test charecters

v_length = STRLEN( v_text ).

v_count = CEIL( v_length / 79 ).

v_after = 0.

DO v_count TIMES.

CLEAR wa_text.

v_sub = v_length - v_after .

  • If v_sub is less than 78

IF v_sub LT 79 .

wa_text-line = v_text+v_after(v_sub).

APPEND wa_text TO it_text.

ELSE.

wa_text = v_text+v_after(79).

APPEND wa_text TO it_text.

v_after = v_after + 79.

ENDIF.

ENDDO.

it_text will hve the charecters divided into 79.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
950

Hello Carles,

Are you trying to split a string, i mean, a variable of TYPE STRING?

You can have a look at the FM: FORMAT_TEXTLINES.

BR,

Suhas

PS: It will not work for variables of TYPE STRING.

Read only

Former Member
0 Likes
950

hi,

Use this Fm in the

ISM_SPLIT_STRING

HR_ES_SPLIT_STRING_TO_4_CHAR50

Read only

Former Member
0 Likes
950

Hi Carles,

Check the below code, it will split the strings each of 10 chars, but it will not split the words.

You can replace all '10' by '79' for your requirement.

Output of this code:

Original String:  this is the example to splt the string based on no of chars !

Splitted Strings:
this is
the
example to
splt the
string
based on
no of
chars !

DATA: w_txt TYPE string,
      w_hold TYPE char10,
      w_end,
      w_exit.

DATA: w_len TYPE i,
      w_do TYPE i VALUE 10,
      w_mod TYPE i,
      w_max TYPE i,
      w_ind TYPE i,
      w_one TYPE i,
      w_two TYPE i,
      w_val TYPE i VALUE 10,
      w_cnt TYPE i VALUE 1.

DATA: BEGIN OF it_tab OCCURS 0,
      txt TYPE char10,
      END OF it_tab.
w_txt = 'this is the example to splt the string based on no of chars !'.
WRITE: / 'Original String: ', w_txt , /.
w_len = strlen( w_txt ).
WRITE: / 'Splitted Strings: '.
WHILE w_cnt < w_len.
  w_do = 10.
  w_ind = w_cnt - 1.
  w_max = w_ind + 10.

  IF w_max > w_len.
    w_do = w_len - w_ind.
    w_end = 'X'.
  ENDIF.

  " Below loop checks for the space at the end of the word
  " If no move to the previous char otherwise it will split the word
  DO.
    IF NOT w_end EQ 'X'.
      w_one = w_ind + w_do - 1.
      w_two = w_one + 1.
    ENDIF.
    IF w_txt+w_one(1) CO ' ' OR
       w_txt+w_two(1) CO ' ' OR
       w_end EQ 'X'.
      w_hold = w_txt+w_ind(w_do).
      it_tab-txt = w_hold.
      CONDENSE it_tab-txt.
      APPEND it_tab.
      EXIT.
    ELSE.
      SUBTRACT 1 FROM w_do.
    ENDIF.
  ENDDO.


  ADD w_do TO w_cnt.
ENDWHILE.

LOOP AT it_tab.
  WRITE: / it_tab-txt.
ENDLOOP.

Hope this helps you.

Regards,

Manoj Kumar P

Edited by: Manoj Kumar on Jan 12, 2009 8:44 AM