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: 

numeric containing in char - how to extract

Former Member
0 Kudos

HI,

Field1 = 'HELLO WORLD 20'.

Field1 contains characters and one number between 01 and 999.

How to extract the number of field1.

Note: the number can be somewhere in that field.

Thx,

Gordon

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

You can try this

Field1 = 'HELLO WORLD 20'.

data : len type i.

data : num(5).

data : count type i.

count = 0.

len = strlen (field1).

do.

if FIELD1+COUNT(1) co '0123456789'.

concatenate num field1+count(1) into num.

endif.

count = count + 1.

if count = len.

exit.

endif.

enddo.

enddo

11 REPLIES 11

Former Member
0 Kudos

Try this logic, First condense the whole string using CONDENSE Field1 NO-GAPS. Then find the string length of the field and push it into a variable. go for DO (string length) TIMES.........ENDDO. and within that loop keep appending the offset of the field and check each character in an if condition, if its other than character, simply push it into another field and you will get all the numeric values.

Former Member
0 Kudos

Hi

You can try this

Field1 = 'HELLO WORLD 20'.

data : len type i.

data : num(5).

data : count type i.

count = 0.

len = strlen (field1).

do.

if FIELD1+COUNT(1) co '0123456789'.

concatenate num field1+count(1) into num.

endif.

count = count + 1.

if count = len.

exit.

endif.

enddo.

enddo

Former Member
0 Kudos

Hi

Good

Function STRLEN only works with character-type fields and returns the length in characters.

Check this example link which will give you idea to split the string.

http://www.sap-img.com/ab039.htm

thanks

mrutyun^

Former Member
0 Kudos

Try the following code,


REPORT  zcl_misc.

DATA:
  w_length TYPE i,
  w_char(1) TYPE c,
  w_string(30) TYPE c,
  w_nums(10) TYPE c,
  w_nset TYPE i,
  w_offset TYPE i.

w_string = 'HE23LLO 20 WORLD'.

CONDENSE w_string NO-GAPS.

w_length = STRLEN( w_string ).

DO w_length TIMES.

  w_char = w_string+w_offset(1).

  SEARCH sy-abcde FOR w_char.

  IF sy-subrc EQ 0.
* Do Nothing.
  ELSE.
    w_nums+w_nset(1) = w_char.
    w_nset = w_nset + 1.
  ENDIF.

  w_offset = w_offset + 1.

ENDDO.

write w_nums.

This will give you the output as "2320". It will work even if there are numeric values anywhere in between.

0 Kudos

And if w_string = '!HE-23LLO+ 20?WORLD'. What will be the result?

You better check on '0123456789' instead of sy-abcde.

Former Member
0 Kudos

Hi,

You can do it easily with the help of a single REPLACE Statement, use it as below;

DATA lv_text TYPE string VALUE 'ab000cdeSDFf34534gh1ASDF234ijk5abc0'.
WRITE : /,'Before Replace :',lv_text.
REPLACE ALL OCCURRENCES OF REGEX '[[:alpha:]]' IN lv_text WITH ''.
WRITE : /,'After Replace :',lv_text.

Output:
Before Replace : ab000cdeSDFf34534gh1ASDF234ijk5abc0

After Replace : 00034534123450

[[:alpha:]]

specifies any alphabet in lower or upper case.

Remember that there is no space between the single quotes in the WITH clause.

Regards

Karthik D

0 Kudos

>

> DATA lv_text TYPE string VALUE 'ab000cdeSDFf34534gh1ASDF234ijk5abc0'.

> WRITE : /,'Before Replace :',lv_text.

> REPLACE ALL OCCURRENCES OF REGEX '[[:alpha:]]' IN lv_text WITH ''.

> WRITE : /,'After Replace :',lv_text.

Nice! However, any commas, periods, question marks etc. will remain in lv_text.

0 Kudos

Hi maen,

For that use the below form of REPLACE;


lv_text = 'asdf1234!@#@!asf~'.
REPLACE ALL OCCURRENCES OF REGEX '\D' IN lv_text WITH ''.

Before Replace : asdf1234!@#@!asf~

After Replace : 1234

This will delete all non-numeric characters including symbols.

Regards

Karthik D

0 Kudos

>

>

> lv_text = 'asdf1234!@#@!asf~'.

> REPLACE ALL OCCURRENCES OF REGEX '\D' IN lv_text WITH ''.

Nice again! I couldn't find any info on OF REGEX, esp what the possibilties are as you used them '\D'.

0 Kudos

>

> Nice again! I couldn't find any info on OF REGEX, esp what the possibilties are as you used them '\D'.

Hi,

The patterns that can be used with REGEX ( Regular expressions) are provided in ABAP Keyword documentation.

In tcode ABAPHELP, it can be accessed as;

ABAP -by theme -> Process Internal Data -> Byte String and Character String Processing -> Regular Expressions -> Syntax of Regular Expressions

see the topic Abbreviations for character sets

Regards

Karthik D

Former Member
0 Kudos

Hello,

Try folowing code

if field1 ca '0123456789'.

l_number = field1+sy-fdpos(3).

Hope this helps,

Rajat