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

Splitting a string field at certain values

Former Member
0 Likes
1,433

Hi all,

  Here is an example of what I have and what I am trying to do.

NOTE_STRING = '1684609022FFYOUR REF=000000428REC FROM=WELLS FARGO BANK FED ID=1210002'

I need to split this value into,

'1684609022FF'

'YOUR REF=000000428REC'

'REC FROM=WELLS FARGO BANK'

'FED ID=1210002'  and write it to a Report.

The decision on where to split the string comes from a table(ZTFI_BAI_TEXTS) where the values are stored as(field name = BAITEXT)  ,

'YOUR REF='

'REC FROM='

'FED ID='.

Thank You,

Anjana


1 ACCEPTED SOLUTION
Read only

michael_kozlowski
Active Contributor
0 Likes
1,334

Use SPLIT similar like below

ostr = '1684609022FFYOUR REF=000000428REC FROM=WELLS FARGO BANK FED ID=1210002'.

str_tmp = ostr.

SPLIT str_tmp  AT 'YOUR REF=' INTO str1 str_tmp.

SPLIT str_tmp  AT 'REC FROM=' INTO str2 str_tmp.

CONCATENATE 'YOUR REF=' str2 INTO str2.

...

4 REPLIES 4
Read only

Former Member
0 Likes
1,334

Press F1 on CS.

Rob

Read only

enric101
Active Contributor
0 Likes
1,334


Hi,

  • First part of string and cut substring. you can use:

             sub1 = NOTE_STRING+0(12).  =>>> this code only if first part is a constant.

  • To find substrings you can use:

               SEARCH NOTE_STRING FOR 'REF='. =>>>> in this case is a hardcode, but you can use ZTFI_BAI_TEXTS-BAITEXT in a loop.

  • After this, you can user instruction split to cut substring before space:

               SPLIT sub3 AT ' ' INTO sub2 sub3.

THis is an example:

DATA: NOTE_STRING type CHAR255 value '1684609022FFYOUR REF=000000428REC FROM=WELLS FARGO BANK FED ID=1210002'.

DATA: sub1 type CHAR255.

DATA: sub2 type CHAR255.

DATA: sub3 TYPE CHAR255.

DATA  zlv_offset type sy-index.

DATA: lenght TYPE sy-index.



sub1 = NOTE_STRING+0(12).



WRITE NOTE_STRING.

WRITE sub1.



SEARCH NOTE_STRING FOR 'REF='.



*Split every field

CALL FUNCTION 'STRING_SPLIT_AT_POSITION'

  EXPORTING

    string                  = NOTE_STRING

    pos                     = sy-fdpos

*   LANGU                   = SY-LANGU

IMPORTING

    STRING1                 = sub2

    STRING2                 = sub3

*   POS_NEW                 =

EXCEPTIONS

   STRING1_TOO_SMALL       = 1

   STRING2_TOO_SMALL       = 2

   POS_NOT_VALID           = 3

   OTHERS                  = 4.



lenght = STRLEN( sub3 ).



*Split only the value

SPLIT sub3+4(lenght) AT ' ' INTO sub2 sub3.



WRITE sub2.

Regards,

Enric

Read only

michael_kozlowski
Active Contributor
0 Likes
1,335

Use SPLIT similar like below

ostr = '1684609022FFYOUR REF=000000428REC FROM=WELLS FARGO BANK FED ID=1210002'.

str_tmp = ostr.

SPLIT str_tmp  AT 'YOUR REF=' INTO str1 str_tmp.

SPLIT str_tmp  AT 'REC FROM=' INTO str2 str_tmp.

CONCATENATE 'YOUR REF=' str2 INTO str2.

...

Read only

former_member183045
Contributor
0 Likes
1,334

A find with a regex would fit here good.

Data: lv_code TYPE STRING,

      lv_ref  TYPE STRING,

      lv_from TYPE STRING,

      lv_fed_id TYPE STRING.


FIND REGEX '^(.*)(YOUR\sREF=.*)(REC\sFROM=.*)(FED\sID=.*)$'
            IN '1684609022FFYOUR REF=000000428REC FROM=WELLS FARGO BANK FED ID=1210002'
            SUBMATCHES lv_code lv_ref lv_from lv_fed_id.


will assign the following values as desired to the variables

lv_code = `1684609022FF`

lv_ref = `YOUR REF=000000428`

lv_from = `REC FROM=WELLS FARGO BANK `

lv_fed_id = `FED ID=1210002`


Regex is not easy to understand if you havent yet worked with it, but you can do nearly every string parsing with it. And in this case the string parsing is rather complicated so I would recommend it.


A good location to playing a bit around is for example: https://regex101.com/ where you get also a nice explanation about the above used regex.


To build the regex dynamically should be not the problem with a for loop on the entries of your table ZTFI_BAI_TEXTS.


Regards, Andreas