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: 

How to find out the index in string.

Former Member
0 Kudos

Hello Experts,

I have a requirement in which i have to find out the index of the last space before the 50th position in a string.

is there any way other than spliting the string.

e.g. if string is - 'aaaa aaaa aaa........ aaa aaa'(length is more than 50)

i want the last space before 50th char.because i want to split the string before last word withing 50 th character

please help.

Thanks,

Shweta

3 REPLIES 3

Former Member
0 Kudos

Hi,

data: w_50th(50).
w_text+0(50) = w_50th
data: i_var type i.

find space IN w_iang MATCH LENGTH i_var.

i_var contans the index value..

Former Member
0 Kudos

Hi Shweta,

You can build up the logic from this below program.

data: line(75) value 'abcdefghijk lmnopqrstuvwxyz zyxwvutsrqponml kjihgfedc ba',

n_line(50),

off type i,

val1(50),

val2(50).

CALL FUNCTION 'STRING_REVERSE'

EXPORTING

STRING = line(50)

LANG = ' '

IMPORTING

RSTRING = n_line

EXCEPTIONS

TOO_SMALL = 1

OTHERS = 2.

IF SY-SUBRC NE 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

SPLIT n_line at space into val1 val2.

off = strlen( val1 ).

off = 50 - off.

write: 'The index of the last space before the 50th position is at ', off.

Hope this may help you.

Regards,

Smart Varghese

0 Kudos

Hello Smart Varghese,

thanks for your reply.

It is working for me.

Regards,

Shweta