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

Former Member
0 Likes
1,624

Hello,

i have a string in this form,

ABCDEFGHI 123456-23 , from which i only want 'ABCDEFGHI'. Is their any SAP method or string function which cuts the string to the point where a number starts.

Thanks..

Shehryar

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,561

data : v_str type string,

v_substr type string,

v_position type i.

v_str = 'ABCDEFGHI 123456-23'.

break-point.

if v_str ca '123456789'.

write 😕 sy-fdpos.

v_position = sy-fdpos.

v_substr = v_str+0(v_position).

endif.

write 😕 v_substr.

check the above code, now V_SUBSTR WILL have ABCDEFGHI value

regards

srikanth

14 REPLIES 14
Read only

Former Member
0 Likes
1,561

try this

split string at ' ' into var1 var2.

Read only

Former Member
0 Likes
1,561

hi ,

data : str1(20),

p1(10),

p2(10).

do like this :

str1= 'ABCDEFGHI 123456-23'.

SPLIT STR1 AT ‘ ’ INTO P1 P2.

write : p1.

Regards

Ashok P

Read only

Former Member
0 Likes
1,561

Hi,

you`ll not be able to do so directly, as SAP doesn`t distinguish between a number and alphabet in string or character datatype. Hence you can try using a variable like the following :

var1 = '123456789'.

After this use the following command :

Find var1 in <yourstring>.

**get the fdpos.

if sy-subrc eq 0.

**split your string using fdpos.

endif.

reward points if helpful.

Regards

Read only

Former Member
0 Likes
1,561
REPORT ABC LINE-SIZE 255.


data : str(25) value 'ABCDEFGHI 123456-23',
       var1(15),
       var2(15).

       split str at ' ' into var1 var2.

       write : var1 , var2.
Read only

Former Member
0 Likes
1,562

data : v_str type string,

v_substr type string,

v_position type i.

v_str = 'ABCDEFGHI 123456-23'.

break-point.

if v_str ca '123456789'.

write 😕 sy-fdpos.

v_position = sy-fdpos.

v_substr = v_str+0(v_position).

endif.

write 😕 v_substr.

check the above code, now V_SUBSTR WILL have ABCDEFGHI value

regards

srikanth

Read only

0 Likes
1,561

hello,

the string can be in any format. how will i go on doing(the logic u told me) if i have a string like, suppose

Bank ABC DEF GHI JK - 04345342

Read only

0 Likes
1,561

Shehryar,

U can use the below logic.

IF L_BANK CA '1234567890-'.

L_POS = SY-FDPOS.

L_STR = L_BANK+0(L_POS).

CONDENSE L_STR.

ENDIF.

Please note you can include all the other characters you to ignore in the string. Also, would like to understand if my replies to your earlier querries helped ?

Regards

Anurag

Message was edited by: Anurag Bankley

Read only

0 Likes
1,561

this program i have given in the earlier post, just added 0 as the no in the IF condition.

you can check this program, this will return the character set till the start of any no.

in this case, "Bank ABC DEF GHI JK - 0"

check this once.

REPORT ZSRIM_TEMP4.

data : v_str type string,

v_substr type string,

v_position type i.

*v_str = 'ABCDEFGHI 123456-23'.

v_str = 'ABC DEF GHI JK - 04345342'.

break-point.

if v_str ca '1234567890'.

write 😕 sy-fdpos.

v_position = sy-fdpos.

v_substr = v_str+0(v_position).

endif.

write 😕 v_substr.

check this program, let us know, your problem solved or not.

Message was edited by: Srikanth Kidambi

Read only

0 Likes
1,561

HELLO,

Try this code,

Regards,

Naimesh

REPORT ZTEST_NP .

data: l_text(50) value 'ABCDEESDF-3232-DAAB-1221'.

DATA: L_LEN TYPE I,

L_CNT TYPE I.

L_LEN = STRLEN( L_TEXT ).

DO L_LEN TIMES.

IF L_TEXT+L_CNT(1) <u>Cn</u> SY-ABCDE.

CLEAR L_TEXT+L_CNT(1).

ENDIF.

L_CNT = L_CNT + 1.

ENDDO.

CONDENSE L_TEXT.

WRITE: L_TEXT.

Read only

Former Member
0 Likes
1,561

Hi Shehryar,

Use the following logic.

data : var1(25),

var2(25),

n type i,

ind.

first calculate the string lentgh usin STRLEN.

n = strlen(string)

Do n times.

if ind is initial.

if stringn(1) = '0' or stringn(1) = '1' or string+n(1) = '2'

and so on..........

ind = sy-index.

endif.

endif.

enddo.

split str at ind into var1 var2.

write : var1 , var2.

Message was edited by: Sachin Dhingra

Read only

Former Member
0 Likes
1,561

Hi,

Consider this code,


REPORT  zztest_prd_1                            .

DATA : it_str TYPE TABLE OF string WITH HEADER LINE.
DATA : it_result TYPE TABLE OF string WITH HEADER LINE.

START-OF-SELECTION.

  it_str = 'ABCDEFGHI 123456-23' .
  APPEND it_str.
  CLEAR it_str.

  it_str = 'ABCDSWHI123456-23' .
  APPEND it_str.
  CLEAR it_str.


  it_str = 'ABSSEFGHI-123456-23' .
  APPEND it_str.
  CLEAR it_str.

<b>  it_str = 'ABC DEF GHI JK - 04345342' .
  APPEND it_str.
  CLEAR it_str.</b>



  LOOP AT it_str.

    CONDENSE it_str.

    IF it_str CA '0123456789'.

      it_result = it_str+sy-fdpos.
      APPEND it_result.
      CLEAR  it_result.

    ENDIF.

  ENDLOOP.

  LOOP AT it_result.

    WRITE : / it_result.


  ENDLOOP.

<b>The output</b>

123456-23
123456-23
123456-23
04345342

Regards,

<b>AS</b>

Read only

Former Member
0 Likes
1,561

Hi shehryar,

Just try this code to split the string when a number is encountered(or started).

data:a(30) type c value 'ABCDEFGHI 123456-23',i type i,j type i,

c(1) type c,k(2) type c,l type i.

i = strlen( a ).

data:begin of itab occurs 0,

b(30),

end of itab.

while j < i.

c = a+j(1).

while k <= 9.

if c co k.

split a at c into table itab.

l = 1.

exit.

endif.

k = k + 1.

endwhile.

if l = 1.

exit.

endif.

k = 0.

j = j + 1.

endwhile.

loop at itab.

write:/ itab-b.

endloop.

Read only

Former Member
0 Likes
1,561

Hi Shehrya,

The method you can use should be split.

data str1(10) type c.

data str2(10) type c.

data str3(10) type c.

str1 = 'ABCDEFGHI 123456-23'.

split str1 at ' ' into str2 str3.

write 😕 str2.

<b>Another way :</b>

str2 = str1(0) + 8.

write : /str2.

In both cases, you will get str2 as ABCDEFGHI .

Thanks and Regards,

Kuanl.

Read only

Former Member
0 Likes
1,561

if v_str ca '0123456789'.

v_fdpos = sy-fdpos + 1.

v_new_str = v_str+0(v_fdpos).

endif.

Regards,

ravi