Application Development 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: 

Extracting from String

0 Kudos

Hi,

if i have a String like 'M[ey]er' how could i extract all information that is not within the brackets so it would give me 'Mer' then ?

Thanks,

1 ACCEPTED SOLUTION

Former Member
0 Kudos

SPLIT string at '[' into x1 x2.

SPLIT x2 at ']' into x3 x4.

concatenate x1 x4 into x5 then x5 will contain the required string 'Mer'

17 REPLIES 17

Former Member
0 Kudos

u can use keyword

SPLIT 'PRABHU' at 'R' into x1 x2 .

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3400358411d1829f0000e829fbfe/frameset.htm

after that u can use concatenate keyword to get into single field.

Regards

Prabhu

Former Member
0 Kudos

SPLIT string at '[' into x1 x2.

SPLIT x2 at ']' into x3 x4.

concatenate x1 x4 into x5 then x5 will contain the required string 'Mer'

0 Kudos

thank you, here is what i got:

 DATA:
   w_string TYPE string ,
   w_sub_string TYPE string,
   w_int TYPE i,
   zaehl TYPE i,
   w_char,
   w_counter TYPE i,
   fl_flag.

  w_string = i_s2.

  w_int = STRLEN( w_string ).
  w_counter = 0.

  DO w_int TIMES.
    zaehl = zaehl + 1.
    w_char = w_string+w_counter(1).

    IF w_char EQ '['.
      zaehl = zaehl - 1.
      WRITE:/ zaehl.
  
      CLEAR fl_flag.
    ENDIF.


    IF fl_flag = 'X'.
      CONCATENATE  w_sub_string w_char INTO w_sub_string.
    ENDIF.

    IF w_char EQ ']'.
      zaehl = 0.
      fl_flag = 'X'.

    ENDIF.

    w_counter = w_counter  + 1.
   
  ENDDO.
  
 WRITE:/ zaehl.
  i_s2 = w_sub_string.

ENDFORM.

Now how do i have to adapt the code that i can do the following:

If i have the string 'Jose[ph]M[ue]ller' then i want to get '[][ph][?][ue][]'.

I will surely reward points generously!

0 Kudos

data str type string.

data str1 type string.

data str2 type string.

data str3 type string.

data str4 type string.

data str5 type string.

str = 'p[riya]nk'.

split str at '[' into str1 str2.

write : / str1, str2.

split str2 at ']' into str3 str4.

write 😕 str3, str4.

if strlen( str1 ) = 1.

str1 = '?'.

else.

str1 = '*'.

endif.

if strlen( str4 ) = 1.

str4 = '?'.

else.

str4 = '*'.

endif.

concatenate str1 '[' str3 ']' str4 into str5.

write 😕 str5.

Message was edited by:

Priyank Jain

0 Kudos

HI..

Just paste this code..

data:

w_string type string value 'ram[mohan]hh[rao]kk[nagam]',

w_len type i,

w_cnt type i.

data:

w_output type string,

w_temp type string.

data:

itab like standard table of w_string with header line.

replace all occurrences of '[' in w_string with ']'.

<b>split w_string at ']' into table itab.</b>

<b>loop at itab .</b>

w_cnt = sy-tabix mod 2.

if w_cnt eq 1.

w_temp = '[*]'.

else.

w_temp = itab.

endif.

<b>concatenate w_output w_temp into w_output.</b>

clear w_temp.

<b>endloop.</b>

<b>write: w_output.</b>

0 Kudos

Thank you, but i am on 4.6 so "all occurrences" wont work, sorry!

0 Kudos

Hi..

try with this..<b>without replace</b> stmt..

data:

w_string(50) type C value 'ram[mohan]hh[rao]kk[nagam]',

w_len type i,

w_cnt type i.

data:

w_output type string ,

w_temp type string.

data:

itab like standard table of w_string with header line.

w_len = strlen( w_string ).

do w_len times.

w_cnt = sy-index - 1.

if w_string+w_cnt(1) eq '['.

w_string+w_cnt(1) = ']'.

endif.

enddo.

<b>split w_string at ']' into table itab.</b>

<b>loop at itab .</b>

w_cnt = sy-tabix mod 2.

if w_cnt eq 1.

w_temp = '[*]'.

else.

w_temp = itab.

endif.

concatenate w_output w_temp into w_output.

clear w_temp.

<b>endloop.

write w_output.</b>

0 Kudos

Hi Rammohan,

this is quite close to what i need, thank you! Could you adjust it once more for me ?

What i need is when i have 'ram[mohan]hh[rao]k[nagam]'. The desired output would be '[][mohan][][rao][?][nagam]'.

Where do i need to change coding ?

Thank you for your help!

0 Kudos

Hi Clemens Leider ,

Now paste this code...modified..solves ur problem..

data:

w_string(50) type C value 'ram[mohan]hh[rao]k[nagam]',

w_len type i,

w_cnt type i.

data:

w_output type string ,

w_temp type string.

data:

itab like standard table of w_string with header line.

w_len = strlen( w_string ).

do w_len times.

w_cnt = sy-index - 1.

if w_string+w_cnt(1) eq '['.

w_string+w_cnt(1) = ']'.

endif.

enddo.

split w_string at ']' into table itab.

loop at itab .

w_cnt = sy-tabix mod 2.

if w_cnt eq 1.

<b>w_len = strlen( itab ).

if w_len = 1.

w_temp = '[?]'.

else.

w_temp = '[*]'.

endif.</b>

else.

w_temp = itab.

endif.

concatenate w_output w_temp into w_output.

clear w_temp.

endloop.

write: w_output.

0 Kudos

Thank you very much, now it works as i wanted it to, but one last question remains - how to use this logic with a string rather than type c ?

0 Kudos

Ram's code works for works for strings as well.

0 Kudos

Hi, i am afraid it doesnt because i get an error stating that at writing positions at fields of type string or xstring no offsetlenght is allowed - it refers to:

if w_string+w_cnt(1) eq '['.

w_string+w_cnt(1) = ']'.

endif.

can you help me ?

0 Kudos

Hi,

not to hijack the thread but see if this helps: Modified version of Ram's code

REPORT ZTEST.

data:
w_string type string value 'ram[mohan]hh[rao]k[nagam]',
*w_string(50) type c value 'ram[mohan]hh[rao]k[nagam]',
w_len type i,
w_cnt type i.

data:
w_output type string ,
w_temp type string.

data:
itab like standard table of w_string with header line.


w_len = strlen( w_string ).

*do w_len times.
*w_cnt = sy-index - 1.
*
*if w_string+w_cnt(1) eq '['.
*w_string+w_cnt(1) = ']'.
*endif.
*
*enddo.
translate w_string using '[]'.
split w_string at ']' into table itab.

loop at itab .
w_cnt = sy-tabix mod 2.

if w_cnt eq 1.

w_len = strlen( itab ).
if w_len = 1.
w_temp = '[?]'.
else.
w_temp = '[*]'.
endif.
else.
w_temp = itab.
endif.

concatenate w_output w_temp into w_output.
clear w_temp.
endloop.

write: w_output.

0 Kudos

Hi ,

Modified <b>...works with string</b> also...

data:

w_string type string value 'ram[mohan]hh[rao]kkk[ram]h[nagam]',

w_len type i,

w_cnt type i.

data:

w_output type string ,

w_temp type string.

data:

itab like standard table of w_string with header line.

translate w_string to upper case.

w_len = strlen( w_string ).

do w_len times.

w_cnt = sy-index - 1.

if w_string+w_cnt(1) na sy-abcde.

concatenate w_temp ']' into w_temp.

else.

concatenate w_temp w_string+w_cnt(1) into w_temp.

endif.

enddo.

w_string = w_temp.

split w_string at ']' into table itab.

loop at itab .

w_cnt = sy-tabix mod 2.

if w_cnt eq 1.

w_len = strlen( itab ).

if w_len = 1.

w_temp = '[?]'.

else.

w_temp = '[*]'.

endif.

else.

w_temp = itab.

endif.

concatenate w_output w_temp into w_output.

clear w_temp.

endloop.

write: w_output.

Former Member
0 Kudos

Hi,

Following is the code in addition to what all has been said by others

data : string(20),

string1(20),

string2(20),

string3(20),

string4(20),

del value '[',

del2 value ']'.

string = 'M[ey]er'.

split string at del into string1 string2.

*write / string1.

split string2 at del2 into string3 string4.

*write / string4.

concatenate string1 string4 into string1.

write / string1. "Gives you Mer

0 Kudos

Hi does this function also respect that there can be more than one [xyz] in a string ?

former_member181962
Active Contributor
0 Kudos

Try this:

REPORT ZTEST.

data: v_name type string value 'Me[yjghje]r'.
data: str1 type string.
data: str2 type string.
data: dummy type string.

split v_name at '[' into dummy str1.
split str1 at ']' into str2 dummy.
concatenate '[' str2 ']' into str2.
replace all occurrences of str2  in v_name with  ''.
write:/ v_name.