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: 

First occurance of a string in a variable

Former Member
0 Kudos

Hi experts,

How can I find the first occurance of the following characters '_S' in a string. I need the first occurance counting it from the end of the string.

1 ACCEPTED SOLUTION

valter_oliveira
Active Contributor
0 Kudos

Hello.


DATA: string TYPE string.
DATA: s TYPE char2 VALUE '_S',
      lenght TYPE i,
      pos TYPE i.
string = 'teste_S123'.
lenght = STRLEN( string ).
SUBTRACT 2 FROM lenght.
pos = lenght.
DO lenght TIMES.
  IF string+pos(2) = s.
    EXIT.
  ENDIF.
  SUBTRACT 1 FROM pos.
ENDDO.

Regards.

Valter Oliveira.

7 REPLIES 7

matt
Active Contributor
0 Kudos

You want the first occurence "counting from the end". That's generally referred to as the last occurence.

Read the help for ABAP key word FIND.

Keep FINDing the string, until you can't find it any more. Then you've the last occurrence.

valter_oliveira
Active Contributor
0 Kudos

Hello.


DATA: string TYPE string.
DATA: s TYPE char2 VALUE '_S',
      lenght TYPE i,
      pos TYPE i.
string = 'teste_S123'.
lenght = STRLEN( string ).
SUBTRACT 2 FROM lenght.
pos = lenght.
DO lenght TIMES.
  IF string+pos(2) = s.
    EXIT.
  ENDIF.
  SUBTRACT 1 FROM pos.
ENDDO.

Regards.

Valter Oliveira.

Former Member
0 Kudos

Edited by: Chandrasekhar Gandla on Jul 8, 2008 11:19 AM

Edited by: Chandrasekhar Gandla on Jul 8, 2008 11:20 AM

Former Member
0 Kudos

Hi,

try this:

data: str type string.

str = 'hs_Sgdtensfdg_Shxgxg'.

search str for '_S'.

if sy-subrc = 0.

write: / 'found at:', sy-fdpos.

endif.

Regards, Dieter

Better is to use:

find '_S' in str.

instead of

search str for '_S'.

Look in the Docu of search / find.

Regards, Dieter

Edited by: Dieter Gröhn on Jul 8, 2008 11:22 AM

Former Member
0 Kudos

hiiii

use like below

FIND FIRST OCCURRENCES OF  'S'  
     IN 'abba' 
     RESULTS result_tab1.

regards

twinkal

Former Member
0 Kudos

Hi,

Try using String operations.

Regards,

Rajitha.

Former Member
0 Kudos

hi,

DATA: text TYPE string VALUE `s_carrid s_connid asdf aasas `,

pos TYPE i.

SEARCH text FOR 's_'.

IF sy-subrc = 0.

pos = sy-fdpos + 2.

SEARCH text FOR 's_' STARTING AT pos

ABBREVIATED AND MARK.

ENDIF.

regard,

sandeep