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: 

How to find number from text string??

Former Member
0 Kudos

Hi All,

Can you tell me how to find/ fetch number from string?

is there any function module for this.?

for example , I have string like "+++++1000000589+++"

and I need to fetch the invoice no 1000000589 from the above string..then How to do that? Letters may come first or number may come first and latter text...

Regards,

Mrunal

11 REPLIES 11

Former Member
0 Kudos

remove all '+' using


replace all occurences of '+' in var with space 

var is ur string variable

Former Member
0 Kudos

Use REPLACE command for alloccurance of '+' , then do a condense XXXXX no-gaps will get you the value.

0 Kudos

Hi,

It's not fixed that always '+' will come , it will be any letter, any text in the string.

Regards,

Mrunal

Former Member
0 Kudos

You can do like this.

The below mentioned is code separate material

DO 18 TIMES.

LV_C = LV_C + 1.

LV_C1 = LV_C - 1.

IF GW_MATNR-MATNR+LV_C1(1) CN '0,1,2,3,4,5,6,7,8,9' .

IF ( ( LV_MAT EQ SPACE ) ) ." and ( lv_prefix eq space ) ) .

LV_PREFIX = 'X'.

CONCATENATE GW_TEMP-PREFIX GW_MATNR-MATNR+LV_C1(1) INTO

GW_TEMP-PREFIX.

ELSE.

LV_SUFFIX = 'X'.

CONCATENATE GW_TEMP-SUFFIX GW_MATNR-MATNR+LV_C1(1) INTO

GW_TEMP-SUFFIX.

ENDIF.

ELSEIF ( ( LV_SUFFIX EQ SPACE ) ) .

LV_MAT = 'X'.

CONCATENATE GW_TEMP-MATNR1 GW_MATNR-MATNR+LV_C1(1) INTO

GW_TEMP-MATNR1.

ELSEIF ( ( LV_MAT EQ 'X' ) ) .

CONCATENATE GW_TEMP-SUFFIX GW_MATNR-MATNR+LV_C1(1) INTO

GW_TEMP-SUFFIX.

ENDIF.

ENDDO.

0 Kudos

Getting better and better!

0 Kudos

Hi,

It would be better if anybody knows function module for this..

Regards,

Mrunal

0 Kudos

Personally, i don't know of any. That means i need to go search for it in SE37. And oh the marvel, you can search as well.

Former Member
0 Kudos

Hi,

You can do the folowing way.

data : w_text(40) type c value 'GIGBIBIK12434FGII', (example)

w_strlen type i,

w_off type i.

w_strlen = strlen (w_text).

Do w_strlen times.

w_off = sy-index - 1.

if w_text+w_off(1) CA '0123456789'.

else.

w_text+w_off(1) = Space.

endif.

enddo.

condense w_text no gaps.

write w_text.

Now you will have only numbers in w_text.

Regards,

Pramod

asik_shameem
Active Contributor
0 Kudos

Hi

No need of FM. You can code like this.

REPORT ztest .

DATA: text   TYPE string VALUE 'AGTH+1000000589++++',
      out    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 out text+i(1) INTO out.
  ENDIF.
ENDDO.

WRITE: out.

~Asik.

0 Kudos

But, imagine the string is: 'Date 12-06-2008 Invoice 10002000 text and another number 123212".

After all the searches, what will be the invoice number? 1206200810002000123212?

It can only work if the only numbers in your string represent the invoice number. That is somethinh you need to be sure of.

Former Member
0 Kudos

Try using regular expressions

for example


DATA text TYPE string.
DATA result_tab TYPE match_result_tab.
DATA wa_result_tab LIKE LINE OF result_tab.

text = `One123!@`.

FIND ALL OCCURRENCES OF REGEX '\d'
     IN text RESULTS result_tab.
LOOP AT result_tab INTO wa_result_tab.
  WRITE: / wa_result_tab-offset,wa_result_tab-length.
ENDLOOP.

here result_tab will contain offset & length of any single digit

one more option you may find usefull is [ [:digit:] ] in place of "\d"