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: 

problem in logic of the Function Module

Former Member
0 Kudos

Hi,

I am working on FM in which i have to pass the value as string and it gives value that if there is a space in the string it will pick the rest of the string in the next value starting from the spcae. here's d code:-



IF TEXT1+20(1) EQ ' '.
          text2 = text1.
          text1 = text1+21(19).
          text3 = text1.

        ELSEIF TEXT1+19(1) EQ ' '.
          text2 = text1.
          text1 = text1+22(18).
          text3 = text1.
        elseif TEXT1+18(1) EQ ' '.
          text2 = text1.
          text1 = text1+23(17).
          text3 = text1.
        elseif TEXT1+17(1) EQ ' '.
          text2 = text1.
          text1 = text1+24(16).
          text3 = text1.
        elseif TEXT1+16(1) EQ ' '.
          text2 = text1.
          text1 = text1+25(15).
          text3 = text1.
        elseif TEXT1+15(1) EQ ' '.
          text2 = text1.
          text1 = text1+26(14).
          text3 = text1.
        elseif TEXT1+14(1) EQ ' '.
          text2 = text1.
          text1 = text1+27(13).
          text3 = text1.
ENDIF.

here text1,text2 and text3 are TYPE QAMR--PRUEFBEMKT with length of 40 characters.

plzz provide me guidlines if there is any thing needed to rectified.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Ricx,

You can use FIND Construct provided in ABAP.

Here is the syntax:


FIND
IN [SECTION OFFSET LENGTH OF]
[IGNORING CASE|RESPECTING CASE]
[IN BYTE MODE|IN CHARACTER MODE]
[MATCH OFFSET ] [MATCH LENGTH ].

You can write something like:



DATA: str(40) TYPE c,
           ctr TYPE i.

str = 'General ABAP'.

FIND FIRST OCCURRENCE OF REGEX ` ` IN str MATCH OFFSET ctr.
write ctr.
*Here 'SPACE exists at index 7 hence *ctr* will have value 7.*
*Now you can get rest string after SPACE by ctr+7[40]*

Hope this helps!

Regards

Shital

5 REPLIES 5

Former Member
0 Kudos

Hi,

Try using regular expression.

Here is an example:

data:l_text TYPE string,

l_result type string.

DATA: moff TYPE i,

mlen TYPE i.

l_text = 'test 1234 4567'.

FIND REGEX ' .*' IN l_text

MATCH OFFSET moff

MATCH LENGTH mlen.

l_result = l_text+moff(mlen).

note that ' .*' means one space follow by any character 0 or more times.

Former Member
0 Kudos

Hi Ricx,

You can use FIND Construct provided in ABAP.

Here is the syntax:


FIND
IN [SECTION OFFSET LENGTH OF]
[IGNORING CASE|RESPECTING CASE]
[IN BYTE MODE|IN CHARACTER MODE]
[MATCH OFFSET ] [MATCH LENGTH ].

You can write something like:



DATA: str(40) TYPE c,
           ctr TYPE i.

str = 'General ABAP'.

FIND FIRST OCCURRENCE OF REGEX ` ` IN str MATCH OFFSET ctr.
write ctr.
*Here 'SPACE exists at index 7 hence *ctr* will have value 7.*
*Now you can get rest string after SPACE by ctr+7[40]*

Hope this helps!

Regards

Shital

0 Kudos

hi,

Now i had changing the logic of the code. is there any function module which count the characters of the string? bcoz i have to make the code globally as what ever the string pass it should be divided a value i.e. 20 and it should count spaces and display the text before a spcae is present.

Plzzz provide me guidelines to solve this problem..

0 Kudos

To get the lenght of string....we can use function STRLEN.

DATA : L_LENGTH TYPE I,

L_TEXT TYPE STRING.

L_TEXT = 'XYZ1234'.

L_LENGTH = STRLEN ( L_TEXT ).

RESULT WILL BE :-

WRITE : 'LENGHT -->' l_lenght.

ANS:- 7

0 Kudos

Hi Ricx ,

Please do not except SAP will provide Function module for all the simple things that programmer might require. There is rare possibility of something like that exists.

Even if you need to write Custom Function module to get String Length it will not be recommended, Somebody will used STRLEN rather than using your Function module.

Hope this clarifies.

Regards

Shital