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

String manipulation

Former Member
0 Likes
4,267

I’m bit new to ABAP and need help on this.

I'm getting a string value from a form field and want to make sure user enters the right value.

User allowed to enter values with or without 2 dashes.

The expected string could be: 9 numbers (‘123-456-789’ or ‘123456789’ ) or 8 numbers (’12-345-678’ or ‘12345678’). No alpha is allowed apart from dash.

If user enters 9 digits with 2 dashes (‘123-456-789’ which is 11 character) or with out 2 dashes (‘123456789’ which is 9 character); I need to strip out the dashes ‘-‘ (if there is any) and then assign each of the numbers to variables e.g. n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5, n6 = 6, n7 = 7, n8 = 8, n9 = 9 to do some calculations.

If user enters 8 digits with 2 dashes (’12-345-678’ which is 10 character) or with out 2 dashes (‘12345678’ which is 8 character long); I need to strip out the dashes ‘-‘ (if there is any) and then add a leading ‘0’ to make 9 digit and then assign each of the numbers to variables e.g. n1 = 0, n2 = 1, n3 = 2, n4 = 3, n5 = 4, n6 = 5, n7 = 6, n8 = 7, n9 = 8 to do some calculations.

Please help me out.

Kind regards.

1 ACCEPTED SOLUTION
Read only

Former Member
1,844
len = strlen(v_string).

if len = 8 or len = 10.
  replace all occurrences of '-' in v_string with space.
   condense v_string.
   concatenate '0' v_string into v_string.
   n1 = v_string+0(1).
   n2 = v_string+1(1).
   n3 = v_string+2(1).
   n4 = v_string+3(1).
   n5 = v_string+4(1).
   n6 = v_string+5(1).
   n7 = v_string+6(1).
   n8 = v_string+7(1).
   n9 = v_string+8(1).
elseif len = 9 or len = 11.
 replace all occurrences of '-' in v_string with space.
 condense v_string.
   v1 = v_string+0(1).
   v2 = v_string+1(1).
   v3 = v_string+2(1).
   v4 = v_string+3(1).
   v5 = v_string+4(1).
   v6 = v_string+5(1).
   v7 = v_string+6(1).
   v8 = v_string+7(1).
   v9 = v_string+8(1).

endif.

Message was edited by:

chandrasekhar jagarlamudi

7 REPLIES 7
Read only

Former Member
0 Likes
1,844

Hi,

Guess there is no std functionality to achieve this directly. You will have to write your own code. May be you can create a Function module.

Cheers

VJ

Read only

Former Member
1,845
len = strlen(v_string).

if len = 8 or len = 10.
  replace all occurrences of '-' in v_string with space.
   condense v_string.
   concatenate '0' v_string into v_string.
   n1 = v_string+0(1).
   n2 = v_string+1(1).
   n3 = v_string+2(1).
   n4 = v_string+3(1).
   n5 = v_string+4(1).
   n6 = v_string+5(1).
   n7 = v_string+6(1).
   n8 = v_string+7(1).
   n9 = v_string+8(1).
elseif len = 9 or len = 11.
 replace all occurrences of '-' in v_string with space.
 condense v_string.
   v1 = v_string+0(1).
   v2 = v_string+1(1).
   v3 = v_string+2(1).
   v4 = v_string+3(1).
   v5 = v_string+4(1).
   v6 = v_string+5(1).
   v7 = v_string+6(1).
   v8 = v_string+7(1).
   v9 = v_string+8(1).

endif.

Message was edited by:

chandrasekhar jagarlamudi

Read only

Former Member
0 Likes
1,844

Hi Nazmul

Please check below code:

data: var_in(11) type c value '12-345-678',
      var_out(9) type n.

replace all occurences of '-' in var_in with space.
condense var_in.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT         = var_in
 IMPORTING
   OUTPUT        = var_out.

break-point.

Check the value of var_out.

Now i guess you can split for further processing..

Kind Regards

Eswar

Read only

Former Member
0 Likes
1,844

Hi ,

Let the name of your string be v_string....then write the following code.

replace all occurrences of '-' in v_string with space.

condense v_string.

overlay v_string with '000000000'.

n1 = v_string+0(1).

n2 = v_string+1(1).

n3 = v_string+2(1).

n4 = v_string+3(1).

n5 = v_string+4(1).

n6 = v_string+5(1).

n7 = v_string+6(1).

n8 = v_string+7(1).

n9 = v_string+8(1).

This will surely solve your problem.

Cheers,

Kirti

Read only

Former Member
0 Likes
1,844

Copy this code and execute it.

data : n1,n2,n3,n4,n5,n6,n7,n8,n9,n10.

PARAMETERS NUM(15) TYPE C.

WRITE NUM COLOR 7.

TRANSLATE NUM USING '- '.

CONDENSE NUM NO-GAPS.

n1 = num(1).

n2 = num+1(1).

n3 = num+2(1).

n4 = num+3(1).

n5 = num+4(1).

n6 = num+5(1).

n7 = num+6(1).

n8 = num+7(1).

n9 = num+8(1).

n10 = num+9(1).

WRITE : /20 n1 COLOR 5.

write : / n2.

write : / n3.

write : / n4.

write : / n5.

write : / n6.

write : / n7.

write : / n8.

write : / n9.

write : / n10.

Read only

Former Member
0 Likes
1,844

hi nazmul,

Welcome to d SDN community

this is a very easy and simple program

i hav tested it and i think this will fulfill ur needs

data: lv_len type i,

lv_string(12)..

****lv_string contains the number string

lv_len = strlen( lv_string ).

case lv_len.

when '8'.

****8 numbers

****concatenate 0

concatenate '0' lv_string into lv_string.

perform data_get using lv_string.

when '9'.

****9 numbers

perform data_get using lv_string.

when '10'.

*****8 numbers with 2 dashes

replace '-' with SPACE into lv_string.

replace '-' with SPACE into lv_string.

condense lv_string no-gaps.

concatenate '0' lv_string into lv_string.

perform data_get using lv_string.

when '11'.

*****9 numbers with 2 dashes

replace '-' with SPACE into lv_string.

replace '-' with SPACE into lv_string.

condense lv_string no-gaps.

perform data_get using lv_string.

endcase.

endform. " sdn_prog1

form data_get using p_string .

data:

n1(1),n2(1),n3(1),n4(1),n5(1),

n6(1),n7(1),n8(1),n9(1).

n1 = p_string+0(1).

n2 = p_string+1(1).

.

.

.

.

****Assign the values to variables

endform.

***Mark points if helpful***

Regards,

Vs

Read only

Former Member
0 Likes
1,844

Thanks a lot to those answred. I'm impressed for quick answers.

Thtanks again all.