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

left function?

Former Member
0 Likes
8,714

Hi ABAPers,

I'm trying to create something in ABAP but I'm stuck right now. I need to do the following:

I have a CHAR that has value: "MZ-OB" -> I need to check if last 2 characters are value "OB". I normally do this in formula editor in transfer rules but now I need to do the same in ABAP. LEFT(2,CHAR) does not work in ABAP and I cannot find the correct syntax for this.

Can somebody help me with this?

thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,977

Another method:

ABAP has a host of string operators that you can use with your IF statements. There exists ca(contains any), cs(contains string), cp(contains pattern), and so on. Your requirement could be coded this way:

DATA: word(5) type c value 'MZ-OB'.

IF word cs 'OB'. ">> This would return true if the word contains string OB.

<do something>

ENDIF.

F1 on the IF statement should get you the help on all of these operators.

Hope this helps.

Sudha

7 REPLIES 7
Read only

Former Member
0 Likes
2,977

use the code below

PARAMETER: ckb1(10).

DATA: len1 TYPE i.

len1 = STRLEN( ckb1 ).

len1 = len1 - 2.

IF ckb1+len1(2) = 'OB'.

MESSAGE i000(vz) WITH ckb1+len1(2).

ENDIF.

U need to ensure that len1 is always GE 2 else you will get a short dump.

-Kiran

*Please mark useful answers

Message was edited by: Kiran Raorane

Read only

Former Member
0 Likes
2,977

Hi Joris,

Check this example :

DATA TIME TYPE T VALUE '172545'.

WRITE TIME.

WRITE / TIME+2(2).

CLEAR TIME+2(4).

WRITE / TIME.

The output appears as follows:

172545

25

170000

First, the minutes are selected by specifying an offset in the WRITE statement. Then, the minutes and seconds are set to their initial values by specifying an offset in the clear statement.

DATA: STRING(25) VALUE ' one two three four',

LEN TYPE I.

LEN = STRLEN( STRING ).

WRITE: STRING, '!'.

WRITE: / 'Length: ', LEN.

CONDENSE STRING.

LEN = STRLEN( STRING ).

WRITE: STRING, '!'.

WRITE: / 'Length: ', LEN.

CONDENSE STRING NO-GAPS.

LEN = STRLEN( STRING ).

WRITE: STRING, '!'.

WRITE: / 'Length: ', LEN.

Output:

one two three four !

Length: 25

one two three four !

Length: 18

onetwothreefour !

Length: 15

Note that the total length of the field STRING remains unchanged, but that the deleted blanks appear again on the right.

Regards,

Laxmi.

Read only

Former Member
0 Likes
2,977

hi Joris,

try this

data: var(5) type c value 'MZ-OB'.

data: var1(2) type c.

data: var2(2) type c.

split var at '-' into var1 var2.

if var2 ca 'OB'.

write: 'Yes'.

else.

Write: 'No'.

endif.

Read only

Former Member
0 Likes
2,977

do like this:

if v_char+3(2) = 'OB'. " If you know the length is always 5 char

endif.

otherwise

if v_char cp '*OB'.

endif.

Regards,

ravi

Read only

Former Member
0 Likes
2,977

Hi Joris,

One method is that you can search for a pattern of substring...

ie SEARCH str_name for 'OB'. and getting the SY-FDPOS(which gives the location of occurence)..

Regards,

SP.

Read only

Former Member
0 Likes
2,978

Another method:

ABAP has a host of string operators that you can use with your IF statements. There exists ca(contains any), cs(contains string), cp(contains pattern), and so on. Your requirement could be coded this way:

DATA: word(5) type c value 'MZ-OB'.

IF word cs 'OB'. ">> This would return true if the word contains string OB.

<do something>

ENDIF.

F1 on the IF statement should get you the help on all of these operators.

Hope this helps.

Sudha

Read only

0 Likes
2,977

ok guys, that helped me out a lot! points have been awarded! thanks