02-13-2007 7:29 AM
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.
02-13-2007 7:51 AM
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
02-13-2007 7:41 AM
hi
you can use string relational operator CA (contains any)...you can capture the position of the numerical values with sy-fdpos..
02-13-2007 7:51 AM
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
02-13-2007 8:14 AM
Thank you, Andreas.
But it may also be possible for the following:
"## Text #"
And we only need "Text #".
Is this possible?
02-13-2007 8:23 AM
02-13-2007 8:26 AM
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
02-14-2007 1:37 AM
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.
02-14-2007 4:40 AM
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.
02-13-2007 8:13 AM
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