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: 

Fetch number from string

Former Member
0 Kudos

Hello Experts,

Can you please suggest the function module which can extract number from a string.

Eg. If I enter AB25, it returns 25.

Thanks in advance.

Ankur

1 ACCEPTED SOLUTION

Former Member
0 Kudos
10 REPLIES 10

former_member189849
Contributor
0 Kudos

hi

if every string have same format use the offset otherwise follow the link http://scn.sap.com/thread/1680390 & for more information google it you get more help.

let me know if u have any q's.

Regards

Mahesh

0 Kudos

HI

Use below code i hope it will help to you.

REPORT zmahe .

DATA: text   TYPE string VALUE 'AGTH+1000000589++++',

text1    TYPE string,

      length TYPE i,

i      TYPE i.

length = STRLEN( text ).

DO length TIMES.

  i = sy-index - 1.

  IF text+i(1) CA '0123456789'.

    CONCATENATE text1 text+i(1) INTO text1.

  ENDIF.

ENDDO.

WRITE: text1.

Regards

Mahesh.

0 Kudos

Thanks Mahesh,

I am able to fetch the number from the above code. But still I would prefer if I can get some function module for this.

As I have several strings in my program and to put a loop on every string with effect the performance.

Anyway thanks a a lot for the quick help.

Ankur

0 Kudos

create a zfunction module for that code whenever do u want just call it.

I hope u can understand .

<< Moderator message - point begging removed >>

Regards

Mahesh.

Message was edited by: Rob Burbank

former_member282968
Contributor
0 Kudos

Dear Ankur,

Please check the below thread:

http://scn.sap.com/thread/1082034

Check the code given by Asik that would help you i guess.

With regards,

Former Member
0 Kudos

0 Kudos

Thanks Kartik,

This solves the purpose.

Regards

Ankur

Former Member
0 Kudos

The fm is FIEB_EXTRACT_NUMBERS

0 Kudos

Hi Ankur,

The FM provided by you is not working properly when string contains "^" character as shown below.

   REPORT  ZNUM_FROM_STRING_SATEESH.
types : begin of tp_tab,
        c(70) type c,
      end of tp_tab.
DATA: text   TYPE string VALUE 'AGTH1000000589hdfkl1221*!@#$34#&*123^45()',
text2    TYPE standard table of tp_tab with header line,
l_numstr type string,
CALL FUNCTION 'FIEB_EXTRACT_NUMBERS'
  EXPORTING
    i_note_to_payee       = text
  tables
    e_numbers             = text2.
If sy-subrc = 0.
  Loop at text2.
    concatenate l_numstr text2 into l_numstr.
  endloop.
  condense l_numstr.
WRITE:/ l_numstr.
endif.

It is giving output as :1000000589122134 but actual o/p should be 100000058912213412345

Use Logic given by Mahesh itself.

former_member585060
Active Contributor
0 Kudos

Hi,

    You can declare a field with N type and move the string data to get the numeric data.

   DATA : v_string   TYPE string,
               v_num     TYPE n LENGTH 8.

   v_string = 'ABSCD0000123'.


v_num = v_string.

WRITE : v_num.

Thanks & Regards

Bala Krishna