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

Find number in string

Former Member
0 Likes
6,398

Hi,

can i find a number in a string?

es.

String is xxxx16080xxxxx

Edited by: Alfonso Manzo on May 26, 2008 3:22 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,213


DATA text TYPE string.

text = 'YTD<J KHGYGJNKLJKKN777GLHGKLB'.

SEARCH text FOR '777'.
IF sy-subrc = 0.

  write:/ sy-fdpos.

ENDIF.

Hi,

can i find a number in a string?

es.

String is xxxx16080xxxxx

Edited by: Alfonso Manzo on May 26, 2008 3:22 PM

10 REPLIES 10
Read only

Former Member
0 Likes
2,213

Hello,

Yes, you can. Use the command FIND .


DATA: patt       TYPE string VALUE `16080`,
  off TYPE i,
  mlen TYPE i.

FIND patt IN  
       `Everybody knows this is nowhere 16080`
       MATCH OFFSET off 
       MATCH LENGTH mlen. 
  IF sy-subrc = 0. 
    WRITE / off. 
    off = off + mlen. 
  ENDIF. 

Regards,

Read only

Former Member
0 Likes
2,214


DATA text TYPE string.

text = 'YTD<J KHGYGJNKLJKKN777GLHGKLB'.

SEARCH text FOR '777'.
IF sy-subrc = 0.

  write:/ sy-fdpos.

ENDIF.

Read only

0 Likes
2,213

I don't know the number.

I have a material description

xxxx919190xxxx and i find a number in description.

es.

xxxx98723xxx -> 98723

xxx 98656 xx -> 98656

256236 -> 256236

Read only

Former Member
0 Likes
2,213

DATA text TYPE string.

text = 'YTD<J KHGYGJNKLJKKN777GLHGKLB'.


IF text CA '1234567890'.  " contains any

  WRITE:/ text.

ENDIF.



Read only

former_member435013
Active Participant
0 Likes
2,213

Hi,

try something like that:

REPORT test.

DATA:

x(132),

i TYPE i,

rc LIKE sy-subrc.

x = 'xxx1280987xxx'.

PERFORM match_integer USING x

CHANGING i

rc.

WRITE: / rc, i.

x = 'xxxxxxxxyyxx9'.

PERFORM match_integer USING x

CHANGING i

rc.

WRITE: / rc, i.

x = '1xx1280987xxx'.

PERFORM match_integer USING x

CHANGING i

rc.

WRITE: / rc, i.

x = 'ABCxxx&&## ff'.

PERFORM match_integer USING x

CHANGING i

rc.

WRITE: / rc, i.

&----


*& Form MATCH_INTEGER

&----


FORM match_integer USING x

CHANGING i

rc.

DATA:

len TYPE i,

off TYPE i.

i = 0.

rc = 1. "default error

len = STRLEN( x ).

IF len = 0.

EXIT.

ENDIF.

  • find start

WHILE off < len.

IF x+off(1) >= '0' AND

x+off(1) <= '9'.

EXIT.

ENDIF.

off = off + 1.

ENDWHILE.

IF off = len.

EXIT.

ENDIF.

  • read int

WHILE off < len.

i = i * 10 + x+off(1).

off = off + 1.

IF x+off(1) < '0' OR

x+off(1) > '9'.

EXIT.

ENDIF.

ENDWHILE.

IF x+off(1) >= '0' AND

x+off(1) <= '9'.

i = i * 10 + x+off(1).

ENDIF.

rc = 0.

ENDFORM. " MATCH_INTEGER

Read only

Former Member
0 Likes
2,213


DATA text TYPE string.

data: num(20) type n.


text = 'YTD<JKHGYGJNKLJKKN777GLHGKLB'.

num = text.

write: num.

ouput = 777.

Read only

BGarcia
Active Contributor
0 Likes
2,213

Hello Alfonso,

In addiction to previous replies, you can also use regular expressions for this.

Here's an example:


DATA: lv_string  TYPE string VALUE 'My Name is 230434 Bruno'.
DATA: lv_pattern TYPE string VALUE '([0-9]+)'.
DATA: lv_result  TYPE string.

FIND REGEX lv_pattern IN lv_string IGNORING CASE
   SUBMATCHES lv_result.

In this case, lv_result variable will get the 230434 number from the string 'My Name is 230434 Bruno'.

Regards,

Bruno

Read only

Former Member
0 Likes
2,213

Hi,

Test the below code :

data : len type i,
       str type string,
       str1 type string,
       final_str type string,
       pos type i.

data : h_type type DD01V-DATATYPE.

parameters : num(20) type c.

str = num.
len = strlen( str ).

pos  = 0.


do len times.

str1 = str+pos(1).

CALL FUNCTION 'NUMERIC_CHECK'
  EXPORTING
    string_in        = str1
  IMPORTING
*   STRING_OUT       =
    HTYPE            = h_type.

if h_type = 'NUMC'.

   concatenate final_str str1 into final_str.

ENDIF.



pos  = pos + 1.

clear str1.
enddo.

write : final_str.

If above works as per your requirement then reward me.

Regards,

Raghu

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,213


data:search(10) type c value '0123456789'.

data:string_val type string.

string_val = 'abdc16080efgghi'.


if string_val cp search.

write: 'Found'.

endif.
Read only

0 Likes
2,213

sorry its like this..

data:search(10) type c value '0123456789'.

data:string_val type string.

string_val = 'abdc16080efgghi'.

if string_val ca search.

write: 'Found'.

endif.