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: 

Extract Numbers from Text Field?

Former Member
0 Kudos

Hi all,

We have a need to extract only the numbers from a text field. Is there a way to do this? For example, in a field we may have any of the following:

"## - Text"

"###.Text"

"# Text"

"Text"

"####Text"

We only need the "Text" from each field but they are no consistent delimiters and in some cases, may not even include any numbers at all.

Thanks in advance.

1 ACCEPTED SOLUTION

andreas_mann3
Active Contributor
0 Kudos

hi,

use fm PREPARE_STRING

sample:

REPORT zz000013 .

PARAMETERS ztext(45) default '## - Text'.

data str(30).

move sy-abcde to str+1.

WRITE: / ztext.

CALL FUNCTION 'PREPARE_STRING'

EXPORTING

i_valid_chars = str

i_xvalid_check = 'X'

i_xchar_repl = 'X'

i_xtoupper = 'X'

CHANGING

c_string = ztext.

CONDENSE ztext NO-GAPS.

write / ztext.

A.

Message was edited by:

Andreas Mann

8 REPLIES 8

Former Member
0 Kudos

hi

you can use string relational operator CA (contains any)...you can capture the position of the numerical values with sy-fdpos..

andreas_mann3
Active Contributor
0 Kudos

hi,

use fm PREPARE_STRING

sample:

REPORT zz000013 .

PARAMETERS ztext(45) default '## - Text'.

data str(30).

move sy-abcde to str+1.

WRITE: / ztext.

CALL FUNCTION 'PREPARE_STRING'

EXPORTING

i_valid_chars = str

i_xvalid_check = 'X'

i_xchar_repl = 'X'

i_xtoupper = 'X'

CHANGING

c_string = ztext.

CONDENSE ztext NO-GAPS.

write / ztext.

A.

Message was edited by:

Andreas Mann

0 Kudos

Thank you, Andreas.

But it may also be possible for the following:

"## Text #"

And we only need "Text #".

Is this possible?

0 Kudos

.

0 Kudos

Hi

Try this:

PARAMETERS ZTEXT(45) DEFAULT '## - Text ##'.

DATA: STR(30),
     STR2 LIKE ZTEXT.

DATA: LEN TYPE I.

MOVE SY-ABCDE TO STR+1.

WRITE: / ZTEXT.

STR2 = ZTEXT.

CALL FUNCTION 'PREPARE_STRING'
     EXPORTING
          I_VALID_CHARS  = STR
          I_XVALID_CHECK = 'X'
          I_XCHAR_REPL   = 'X'
          I_XTOUPPER     = 'X'
     CHANGING
          C_STRING       = STR2.

CONDENSE STR2.
LEN = STRLEN( STR2 ).


SEARCH ZTEXT FOR STR2(LEN).
IF SY-SUBRC = 0.
  ZTEXT = ZTEXT+SY-FDPOS.
ENDIF.

WRITE / ZTEXT.

I've just only modified the Andreas's code, anyway u should know in which side of the string is the numerical parts.

Max

0 Kudos

I'm sorry. Perhaps to be even more clear, we just need to remove the initial numbers. It's possible to have:

"###. # Text ## text ### Text" and we would need "# Text ## text ### Text"

or

"## - # Text ## text" and we would need "# Text ## text"

or

"## Text # text" and we need "Text # text"

or even no number at all!

"Text # text" and we need that entire thing.

Your suggestions are very helpful so far and I am seeing how best to modify it.

0 Kudos
DATA: string(40) TYPE c VALUE '###. # Text ## text ### Text',
           string1(10) TYPE c ,
           string2(10) TYPE c .
          string3(10) TYPE c .

SPLIT string AT DELIMITER INTO string1 string2 string3.
IF string1 CA '0123456789'.
WRITE string2.
If NOT string3 IS INITIAL.
WRITE String3.
ENDIF.
ELSE.
WRITE String1.

Delimiters may be . , ! / ' etc.

Hope this may help u.

Former Member
0 Kudos

John,

Pls. check .

data: v_char_field(100),

v_qty_num type rl03t-pickm.

call function 'MOVE_CHAR_TO_NUM'

exporting

chr = v_char_field

importing

num = v_qty_num

exceptions

convt_no_number = 1

convt_overflow = 2.

if sy-subrc <> 0.

"write/throw error message

endif.

2)QSS0_CHAR_FLTP_CONVERSION.

Pls. mark if useful