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

string operation extract numerical from string

arpit_shah
Contributor
0 Likes
1,289

Hi Experts,

how to extract numerical from string,

suppose,

if string = 'D050S114D7067'

then i want the numerical data of string,

means o/p = 0501147067

or

if string = YG1100KWC32

then o/p should be 110032

how to extract numerical characters from string?

Regards,

Arpit Shah

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,180

HI,

assign directly like below.


DATA: lv_string(10) VALUE 'C9A0041254',
lv_number(10) TYPE n.
 
lv_number = lv_string.
WRITE:/ lv_number.

rgds,

bharat.

6 REPLIES 6
Read only

Former Member
0 Likes
1,181

HI,

assign directly like below.


DATA: lv_string(10) VALUE 'C9A0041254',
lv_number(10) TYPE n.
 
lv_number = lv_string.
WRITE:/ lv_number.

rgds,

bharat.

Read only

0 Likes
1,180

Hi Bharat,

sorry, i hv write something different,

but my requirement is,

if string = 'D050S114D7067'

then i want the numerical data of string,

i want to split after numerical if any character is found.

means o/p should be = 050

or

if string = YG1100KWC32

then o/p should be 1100.

Read only

BGarcia
Active Contributor
0 Likes
1,180

Hello Arpit.

Using regular expressions, you can achieve that like this:


DATA: lv_string  TYPE string VALUE 'D050S114D7067'.
DATA: lv_pattern TYPE string VALUE '([0-9]+)'.
DATA: lv_result  TYPE string.
DATA: lt_result  TYPE match_result_tab.
DATA: ls_result  LIKE LINE OF lt_result.

FIND ALL OCCURRENCES OF REGEX lv_pattern IN lv_string RESULTS lt_result.
LOOP AT lt_result INTO ls_result.
  CONCATENATE lv_result lv_string+ls_result-offset(ls_result-length) INTO lv_result.
ENDLOOP.

Kind regards.

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
1,180

Hi Arpit,

You can use Regular Expressions(REGEX) to do it.

"As of release 7.0, ABAP supports extended regular expressions in accordance with POSIX standard 1003.2. Regular expressions can be used after the addition REGEX of the statements FIND and REPLACE for searching in character strings."

Font SAP help.

Do as follow:


...

string = 'D050S114D7067'.

REPLACE ALL OCCURRENCES OF REGEX '[:A-Z:]' IN v_data WITH SPACE.

Now string = 0501147067.

I encourage you to always use regular expressions in your programs.

For more information about regular expressions see:

[www.regular-expressions.info|www.regular-expressions.info]

[Regular Expression on Wikpedia|en.wikipedia.org/wiki/Regular_expression].

Greetings.

Marcelo Ramos

Read only

0 Likes
1,180

Arpit,

if your string variable may contain characters in Uppercase and Lowercase you can use the following syntax.


...

string = 'd050S114d7067'

REPLACE ALL OCCURRENCES OF REGEX '[:A-Za-z:]' IN v_data WITH SPACE.

Greetings,

Marcelo Ramos

Read only

arpit_shah
Contributor
0 Likes
1,180

i hv do it myself....

first i hv taken all numercal data and after that take first 2 character.

thanks and rgds,

Arpit