how do i break a string?

ombhai
ombhai used Ask the Experts™
on
i amretrivein customer cod from customer master table and i've to put taht customer code in the internal tablein this way for e.g the cutomer code is ZG001. so i've to send this zg001 to the internal table field as ZG and 001 seperately.the gist is i've to break the customer number which is a alpha numeric field into alpha and numeric seperately so that i can add it to the diifernt fields in internal table.
can anybody help me with the code i want the full programm for it i.e how to break alphanumeric into alpha and numeric.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
ombhai

There are several ways to do it.  One neat way is as follows:-

Data: begin of customer_code,
       Prefix(2),
       Suffix(3),
      end of customer_code.

(You could define the suffix as some kind of numeric but if it ever got filled with alphas it would error)

Then all you do is move the value into the field customer-code and you can then access the individual fields.  For example if the data is in field KNA1-kunnr you can say:-

move kna1-kunnr to customer_code.

Write: / customer_code-prefix,
         customer_code-suffix.

or you can just move them to different fields.

An alternative is to use an offset.  For example, the following would do the same thing:

write: / kna1-kunnr(2),
       / kna1-kunnr+2(3).

or use the same specifications in a move statement.

Mike
 
Commented:
I give the working code, which is already tested.
The ALPHABET & NUMERIC portion of customer text is not based on size (size free), but it does have assumption that there will be ALPHA & NUMERIC, and ALPHA always comes first, followed by NUMERIC.


=======================================================
report SPLIT_ALPHA_&_NUMBER.

start-of-selection.
data: KUNNR like VBAK-KUNNR,
      ALPHA(10)  TYPE C,
      NUMBER(10) TYPE C,
      LEN   type I,
      POSITION_OF_NUMBER type I.

KUNNR = 'ZGF001'.

* Get the location of first number
LEN = strlen( KUNNR ).
DO LEN TIMES.
 POSITION_OF_NUMBER = SY-INDEX.
 if KUNNR+SY-INDEX(1) ca '0123456789'.
   exit.
 endif.
ENDDO.

* Split into ALPHA-portion & Number-portion of customer code
ALPHA = KUNNR+0(POSITION_OF_NUMBER).
LEN = LEN - POSITION_OF_NUMBER.
NUMBER = KUNNR+POSITION_OF_NUMBER(LEN).
WRITE: / ALPHA, NUMBER.
=======================================================

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial