cancel
Showing results for 
Search instead for 
Did you mean: 

SPLIT String

Former Member
0 Kudos

Hi All,

Can anyone tell me how do i separate following:

01, Rupesh, Mhatre, "Mhatre, Rupesh", 23.08.2008

as

01

Rupesh

Mhatre

Mhatre, Rupesh

23.08.2008

Thanks in Advance

Regards,

Rupesh

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Try this:

DATA: g_text  TYPE string,
      g_text2 type string.

DATA: it_result_tab TYPE match_result_tab,
      wa_result_tab TYPE match_result,
      g_start  TYPE syindex,
      g_end    TYPE syindex,
      g_length TYPE syindex.

g_text = '01, Rupesh, Mhatre, "Mhatre, Rupesh", 23.08.2008'.


FIND ALL OCCURRENCES OF '"' IN g_text RESULTS it_result_tab.
IF sy-subrc EQ 0.

  READ TABLE it_result_tab INDEX 1 INTO wa_result_tab.
  IF sy-subrc EQ 0.
    g_start = wa_result_tab-offset.
  ENDIF.

  READ TABLE it_result_tab INDEX 2 INTO wa_result_tab.
  IF sy-subrc EQ 0.
    g_end = wa_result_tab-offset.
  ENDIF.

  g_length = g_end - g_start + 1.

  g_text2 = g_text+g_start(g_length).

  replace g_text2 in g_text with ''.


* Then do the splitting on G_TEXT

ENDIF.

Former Member
0 Kudos

Hi Maen,

Thanks a lot your brilliant solution.

But for SAP R/3 4.7C there is no support for

FIND ALL OCCURRENCES.

Hence, somebody can try this. It gets the offset values of all the ' " ' in a string.

DO len TIMES.

SEARCH g_text(sy-index) FOR ' " '

IN CHARACTER MODE

STARTING AT sy-index.

IF sy-subrc = 0.

off = sy-index - 1.

wa_temp-l_offset = off.

APPEND wa_temp TO l_temp.

CLEAR: off, wa_temp.

ENDIF.

ENDDO.

Once Again Thanks everybody.

& Nothing is impossible in SAP world &

Happy Coding.

Regards,

Rupesh

Answers (9)

Answers (9)

Former Member
0 Kudos

Hi Rupesh ,

Look into the below code its working as per your requirement.

DATA : STR1 TYPE STRING.
TYPES : BEGIN OF TY_ITAB,
       W_S1(100) TYPE C,
       END OF TY_ITAB.
DATA : GT_ITAB TYPE TABLE OF TY_ITAB,
       GT_JTAB TYPE TABLE OF TY_ITAB,
       WA_ITAB TYPE TY_ITAB,
       GT_ITAB1 TYPE TABLE OF TY_ITAB,
       WA_ITAB1 TYPE TY_ITAB,
       WA_ITAB2 TYPE TY_ITAB,
       GT_ITAB2 TYPE TABLE OF TY_ITAB.
DATA LEN TYPE I.

WA_ITAB-W_S1 = '01,hyderabad,"vikram,chandu",03.03.1983'.
APPEND WA_ITAB TO GT_JTAB.
WA_ITAB-W_S1 = '01,hyderabad,"mukesh,kumr",03.03.1983,"kumar"'.
APPEND WA_ITAB TO GT_JTAB.
WA_ITAB-W_S1 = '01,hyderabad,"chaitu,hanmantu",03.03.1983'.
APPEND WA_ITAB TO GT_JTAB.
CLEAR WA_ITAB.
LOOP AT GT_JTAB INTO WA_ITAB2.
  CLEAR GT_ITAB.
  SPLIT WA_ITAB2-W_S1 AT '"' INTO TABLE GT_ITAB.
  
  LOOP AT GT_ITAB INTO WA_ITAB.
    CLEAR : GT_ITAB1[],LEN,WA_ITAB1.
    LEN = STRLEN( WA_ITAB ) - 1.
    IF ( WA_ITAB-W_S1(1) EQ ',' ) OR ( WA_ITAB-W_S1+LEN(1) EQ ',').
      SPLIT WA_ITAB-W_S1 AT ',' INTO TABLE GT_ITAB1.
      LOOP AT GT_ITAB1 INTO WA_ITAB1.
        APPEND WA_ITAB1 TO GT_ITAB2.
      ENDLOOP.
    ELSE.
      WA_ITAB1-W_S1 = WA_ITAB-W_S1.
      APPEND WA_ITAB1 TO GT_ITAB2.
    ENDIF.
  ENDLOOP.
ENDLOOP.
LOOP AT GT_ITAB2 INTO WA_ITAB.
  WRITE WA_ITAB-W_S1.
ENDLOOP.

This works for multiple strings,

please provide ur suggestions

regards

Former Member
0 Kudos

Thanks Mukesh it really worked.

But I had implemented Maen's solution as it suited my requirement.

Also, I would like to thank Maen for the solution. It works very well in ECC 6.0 but my client is having SAP R/3 4.7C version.

Thanks a lot all of you.

Regards,

Rupesh

Edited by: Rupesh Mhatre on Oct 9, 2008 8:39 AM

Former Member
0 Kudos

hi ,

try this ... its working ..


data :  str type  string ,
  com_pos TYPE i,
     str_tab type tline_t,
     wa_str like LINE OF str_tab.

str = '01, Rupesh, Mhatre, "Mhatre, Rupesh", 23.08.2008'.

while str is NOT INITIAL.

CLEAR com_pos .

if str(1) = ',' .
  str = str+1.
  ENDIF.
  SHIFT str LEFT DELETING LEADING space.
if str(1) = '"' .
  str = str+1.
find FIRST OCCURRENCE OF '"' in str MATCH OFFSET com_pos .
else.
  find FIRST OCCURRENCE OF ',' in str MATCH OFFSET com_pos .
ENDIF.

if com_pos is INITIAL .
  wa_str-tdline = str .
  CLEAR str.
  else.
  wa_str-TDLINE = str(com_pos).
str = str+com_pos.
if str(1) = '"' .
str = str+1.
ENDIF.

ENDIF.
APPEND wa_str to str_tab.
ENDWHILE.

this will work for any case ... means if " value is there in any place also ....

Edited by: ganesh prasad on Oct 8, 2008 11:18 AM

Former Member
0 Kudos

split <variable> at ',' into table <internaltable>.

this will work for sure

Former Member
0 Kudos

Below code is giving the output you expected . Check and let us know.

DATA: n(2) type n,

str1 TYPE string,

str2 TYPE string,

str3 TYPE string,

str4 type string,

date(10) type c,

text type string.

text = '01, Rupesh, Mhatre, "Mhatre, Rupesh", 23.08.2008'.

REPLACE ALL OCCURRENCES OF REGEX '"' IN text WITH ''.

condense text no-gaps.

SPLIT text AT ',' INTO: n str1 str2 str3 str4 date.

concatenate str3 ',' str4 into str3.

write : n , str1, str2, str3, date.

output :

01 Rupesh Mhatre Mhatre,Rupesh 23.08.2008

Former Member
0 Kudos

No Karthik I tried doing this but didn't worked

Regards,

Rupesh

Former Member
0 Kudos

its working for me , how come your not getting ???? . What output you are getting. ( Copy the code , activate it and paste the output ).

Former Member
0 Kudos

Are you looking to get the O/P as line ???????

Former Member
0 Kudos

First of all many thanks Karthik,

But in my case it's not sure that comma would exist in which field. It can exist in any or every field so I cannot split it into two vars and then concatenate it again as you said

Hence forth it is not working.

Regards,

Rupesh

Former Member
0 Kudos

Convey your thanks by rewarding points for all valuable inputs . Do you know the structure of the input string say first 2 will be on number 3-10 will be of first name 10-16 of last lane like that.......

Former Member
0 Kudos

Yes I know that but again it is not sure that first name will be

3-10 characters and last name 11-18 characters so how do i identify that?

Regards,

Rupesh

Former Member
0 Kudos

If the first and last name is always like "nnnn,mmmm" and with the double quotes, filter out that part into another string. Then do the splitting at the remaining text.

And for flexibility it's best to use a SPLIT .. INTO TABLE and not into separate variables.

Former Member
0 Kudos

if you are not clear about the structure , it's tough !!!! .Ask the guy how gave you the requirement if you dnt know .

former_member188685
Active Contributor
0 Kudos

This can be done using the Regular expressions. just search in SDN for Regular Expressions usage.

Former Member
0 Kudos

Use -

SPLIT string AT ',' INTO TABLE itab.

Regards,

Aparna

Former Member
0 Kudos

DATA: n(2) type n,

str1 TYPE string,

str2 TYPE string,

str3 TYPE string,

date type datum,

comma type c value ',',

text type string.

text = '01, Rupesh, Mhatre, "Mhatre, Rupesh", 23.08.2008'

SPLIT text AT comma INTO: n str1 str2 str3 date.

Former Member
0 Kudos

Hi,

Try like this,,,


split lv_string at ',' into table <internaltable>.

Hope it will helps

Former Member
0 Kudos

I tried but it treats Mhatre, Rupesh as two fields instead of one field

Regards,

Rupesh

Former Member
0 Kudos

Press F1 on statement SPLIT. And all will be explained.

edit:

The only problem will be the splitting of "Mhatre, Rupesh". You don't want that to be split, so you'll have to figure out a solution for that.

Edited by: Maen Anachronos on Oct 6, 2008 2:05 PM

Former Member
0 Kudos

Hi Maen,

After trying the code given by you, I came to know that find all occurrences does'nt exists for SAP version 4.7C.

Is there anything else instead of 'find all occurrences' that I can use.

Regards,

Rupesh

Former Member
0 Kudos

Try with SEARCH.

Former Member
0 Kudos

Hi , Try this code. It works for me as you asked.


REPORT  zjtest_text2.

DATA: chr TYPE char50,
      txt1 LIKE chr,
      txt2 LIKE chr,
      txt3 LIKE chr.

chr = '01, Rupesh, Mhatre, "Mhatre, Rupesh", 23.08.2008'.

CONDENSE chr.

txt1 = chr.

DO.
  SPLIT txt1 AT ',' INTO txt1 txt2.

  IF txt1 CA '"'.
    REPLACE '"' INTO txt1 WITH space.
    SPLIT txt2 AT '"' INTO txt3 txt2.

    CONCATENATE txt1 ',' txt3 INTO txt1.
    CONDENSE txt1.
  ENDIF.
  shift txt1 left deleting leading space.
  WRITE / txt1.

  txt1 = txt2.
  IF txt2 IS INITIAL.
    EXIT.
  ENDIF.

ENDDO.

Regards,

Jey