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

Numeric portion of string

Former Member
0 Likes
1,960

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.

19 REPLIES 19
Read only

Former Member
0 Likes
1,926

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

Read only

Former Member
0 Likes
1,926

Hi,

Declare one numeric field like below

data: v_num(5) type n.

move 'ABC12345' to v_num.

write:/ v_num.

Read only

Former Member
0 Likes
1,926

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....

Read only

Former Member
0 Likes
1,926

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

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,926

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.

Read only

Former Member
0 Likes
1,926

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.

Read only

Former Member
0 Likes
1,926

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

Read only

Former Member
0 Likes
1,926

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.

Read only

RemiKaimal
Active Contributor
0 Likes
1,926

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

Read only

ThomasZloch
Active Contributor
0 Likes
1,926

Nice example how one line of Regex (see Manish's reply) makes a lot of complicated code obsolete.

Thomas

Read only

0 Likes
1,926

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

Read only

0 Likes
1,926

Very nice...I will have to dig more into this topic

Read only

0 Likes
1,926

>

> 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

Read only

0 Likes
1,926

He'll probably confess that he's working with 4.6C and there goes the nice solution...

Read only

0 Likes
1,926

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?

Read only

0 Likes
1,926

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

Read only

0 Likes
1,926

Karthik you rock )

Still this thread is not closed !!!!

Read only

former_member585060
Active Contributor
0 Likes
1,926

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

Read only

0 Likes
1,926

Not quite, it still has 'ABC12345', while lv_rfqnumber now has '0000012345'.

Thomas