‎2009 Jun 15 12:54 PM
Hello -
I want to extract the numeric portion of an alphanumeric string.
Ex: ABC12345 - I want to get back 12345.
How do I go about this.
‎2009 Jun 15 1:02 PM
U can use the string operations.
see the below example
data: l_var1(9) type c,
l_var (9) type c.
l_var1 = 'ABC12345'.
l_var2 = l_var1+3(6)
output l_var2 = 12345
‎2009 Jun 15 1:03 PM
Hi,
Declare one numeric field like below
data: v_num(5) type n.
move 'ABC12345' to v_num.
write:/ v_num.
‎2009 Jun 15 1:07 PM
Hi,
loop at your varaible... The first instance you come with the numeric characters put it ina diifernt variable..
or,
You can even declare a numeric variable and assign your value directly to it....
‎2009 Jun 15 1:09 PM
Hi,
Try out this sample code..
DATA lv_wa TYPE string.
DATA lv_wab TYPE char30 VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
DATA lv_ind TYPE i.
DATA lv_index TYPE i.
data text type string.
lv_wa = 'ABCD123456'.
lv_ind = 0.
DO 10 TIMES.
IF lv_wa+lv_ind(1) CN lv_wab.
CONCATENATE text lv_wa+lv_ind(1) INTO text.
ENDIF.
lv_ind = lv_ind + 1.
ENDDO.
U may have to make some small changes.but the logic will work..
Regards
Ansari
‎2009 Jun 15 1:10 PM
im not ware of any Fm
But u can build a logic for this
data:len type i,
number(10) type c value '1234567890',
val type string,
output type string.
val = 'ABC12345'.
len = strlen( val ).
do len times.
sy-index = sy-index - 1.
if val+sy-index(1) ca number.
concatenate output val+sy-index(1) into output.
endif.
enddo.
write output.
‎2009 Jun 15 1:13 PM
Hi Vijay, try the example code. It would work for all input strings irrespective of positioning of alphabets.
DATA str TYPE string.
str = 'ABC12345DEF789'.
WRITE:/ str.
REPLACE ALL OCCURRENCES OF REGEX '[a-zA-Z]*' IN str WITH space.
WRITE:/ str.
‎2009 Jun 15 1:14 PM
See the sample code..
data : w_string type string value ' ABC12345',
count type i,
w_strlen type I.
w_strlen = strlen ( w_string ).
Do w_strlen type.
if w_string+count(1) CO sy-abcde.
count = count + 1.
else.
Exit.
endif.
ENDDO.
w_string = w_string+count(w_strlen - count ).
write s_string.
_OUTPUT :--12345_
Regards,
Prabhudas
‎2009 Jun 15 1:14 PM
hi,
Check this,
PARAMETERS : P_STR(20) TYPE C.
DATA: LEN TYPE I,
N TYPE I.
DATA: OUT(20).
LEN = STRLEN( P_STR ).
DO LEN TIMES.
IF P_STR+N(1) CA '1234567890'.
CONCATENATE P_STR+N(1) OUT INTO OUT.
ENDIF.
N = N + 1.
ENDDO.
WRITE OUT.
.
Regards,
R K.
‎2009 Jun 15 1:15 PM
Hello,
Check :
DATA:
len TYPE i,
i TYPE i VALUE 0,
dtype LIKE dd01v-datatype.
PARAMETERS valuein(70).
len = STRLEN( valuein ).
WHILE i <= len.
CALL FUNCTION 'NUMERIC_CHECK'
EXPORTING
string_in = valuein+i(1)
IMPORTING
htype = dtype
.
IF dtype = 'NUMC'.
WRITE: valuein+i(1) NO-GAP.
ENDIF.
i = i + 1.
ENDWHILE.
Input : 0ABC12DE3
output : 0123
Cheers,
Remi
‎2009 Jun 15 1:33 PM
Nice example how one line of Regex (see Manish's reply) makes a lot of complicated code obsolete.
Thomas
‎2009 Jun 15 1:43 PM
And one more simplest one
REPLACE ALL OCCURRENCES OF REGEX '\D' IN str WITH ''.Actually Manish's code will replace all other characters with space, so we need to actually erase the space also so give a empty double apostrophe . ( Note that '' doesnot contains any spaces in between )
Regards
Karthik D
‎2009 Jun 15 1:46 PM
‎2009 Jun 15 1:49 PM
>
> Very nice...I will have to dig more into this topic
While we are digging deep into this topic the OP has gone fishing it seems, no follow up messages
Regards
Karthik D
‎2009 Jun 15 1:56 PM
He'll probably confess that he's working with 4.6C and there goes the nice solution...
‎2009 Jun 15 2:02 PM
hey Karthik, for some reason, replace regex with space is not really putting spaces in the string. (it is replacing with null i think)
REPLACE ALL OCCURRENCES OF REGEX '[a-zA-Z]' IN str WITH space.
What can be the reason behind this?
‎2009 Jun 16 3:52 AM
Yes, I dont know why. But your statement only removes alphabets while my statement removes all other characters including symbols except digits.
Regards
Karthik D
‎2009 Jun 16 9:53 AM
‎2009 Jun 15 1:38 PM
Hi,
If the numeric content is present at once , i.e after text or before text
Just declare a variable of Numeric type assign the alphanumeric variable to it.
DATA : lv_rfqnumber(10) TYPE n,
lv_message(132) TYPE c.
lv_message = 'ABC12345'.
lv_rfqnumber = lv_message.
WRITE :/ lv_message.lv_message will have value 12345.
Regards
Bala Krishna
‎2009 Jun 15 1:42 PM
Not quite, it still has 'ABC12345', while lv_rfqnumber now has '0000012345'.
Thomas