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

Getting number from string?

Former Member
0 Likes
4,428

HI.

I have a question.

I'd like to get just number from string that consist of numbers and char.

for example. String A1313.33BBC

In this string, i want to get 1313.3. Is there any method for this?

thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,896

Hi,

Use the below code;

Data : lv_string type string.
Data : lv_num type string.
lv_string = 'A1313.33BBC'.
lv_num = lv_string.
REPLACE ALL OCCURRENCES OF '[[:alpha:]]' IN lv_num WITH ''.

This will remove all other characters other than numbers and decimal points, now lv_num will have the numeric value.

Check and revert back.

Regards

Karthik D

8 REPLIES 8
Read only

Former Member
0 Likes
3,896

Re: How to find number from text string??

Posted: Oct 10, 2008 1:21 PM in response to: Mrunal Mhaskar Reply

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.

Read only

Former Member
0 Likes
3,896

Hello

Try:


data: str1(20),
      str2(60).

str1 = 'A1313.33BBC'.
str2 = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.
TRANSLATE str1 USING str2.

Read only

Former Member
0 Likes
3,896

Hi,

Check the below code.

DATA: v_str TYPE string VALUE 'A1313.33BBC',

v_str1 TYPE string,

v_str2 TYPE string.

SPLIT v_str AT '.' INTO v_str1 v_str2.

REPLACE ALL OCCURRENCES OF REGEX '\D' IN v_str1 WITH ''.

REPLACE ALL OCCURRENCES OF REGEX '\D' IN v_str2 WITH ''.

IF sy-subrc NE 0.

REPLACE ALL OCCURRENCES OF REGEX '\D' IN v_str WITH ''.

ELSE.

CONCATENATE v_str1 v_str2 INTO v_str SEPARATED BY '.'.

ENDIF.

WRITE:/ v_str.

Regards,

Kumar Bandanadham

Read only

Former Member
0 Likes
3,897

Hi,

Use the below code;

Data : lv_string type string.
Data : lv_num type string.
lv_string = 'A1313.33BBC'.
lv_num = lv_string.
REPLACE ALL OCCURRENCES OF '[[:alpha:]]' IN lv_num WITH ''.

This will remove all other characters other than numbers and decimal points, now lv_num will have the numeric value.

Check and revert back.

Regards

Karthik D

Read only

0 Likes
3,896

You can also do as;

Data : lv_string TYPE string, lv_num type string.
lv_string = 'A1313.33BBC'.
FIND REGEX '(\d*[.]\d*)' IN lv_string SUBMATCHES lv_num.

This will also give the same result.

Regards

Karthik D

Read only

0 Likes
3,161

The proper syntax is:

REPLACE ALL OCCURRENCES OF REGEX '([[:alpha:]])'

 

Read only

Former Member
0 Likes
3,896

Hi Kim,

just go through the fiollowing code... this is working exactly the way you want....

i hope u will get the solution : )

REPORT ZTEST1.

data: f1 type string,

result type string,

count type i,

cnt type i,

final type string.

cnt = 0.

f1 = 'AAA1246VGB,23123'.

count = strlen( f1 ).

if count is not initial.

do count times.

result = f1+cnt(1).

if result CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

clear result.

else.

concatenate final result into final.

endif.

cnt = cnt + 1.

enddo.

endif.

write:/ final.

OUTPUT is 1246,23123

Thanks & Regards

Ashu Singh

Read only

viquar_iqbal
Active Contributor
0 Likes
3,896

Hi

DATA: v_str TYPE string VALUE 'AA1313.33BBC',
vstr1(6) TYPE n,
vstr2 TYPE p .
vstr1 = v_str.
vstr2 = vstr1.
WRITE  vstr2 NO-GROUPING ROUND 2 DECIMALS 1 .

This would give you the required output

Thanks

Viquar Iqbal