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

How to split the string?

Former Member
0 Likes
920

Hi Experts,

Can anyone explain me with code how to separate the single character from the given input string?

For Example: Input String str = 'RAGHU'

Output should be:

R

RA

RAGH

RAGHU

RAGH

RAG

RA

R

Can anyone help me with code for above required output?

Thanks in Advance,

Regards,

Raghu.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
897

TRY THIS


DATA : TEXT(10) VALUE 'RAGHU',
       LEN TYPE I,
       POS TYPE I,
       LNG TYPE I,
       VCH(10).
LEN = STRLEN( TEXT ).
LNG = LEN * 2.
LNG = LNG - 1.
DO LNG TIMES.
IF SY-INDEX LE LEN.
POS = POS + 1.
ELSE.
POS = POS - 1.
ENDIF.
VCH = TEXT+0(POS).
WRITE : / VCH.
ENDDO.

REGARDS

SHIBA DUTTA

8 REPLIES 8
Read only

Former Member
0 Likes
898

TRY THIS


DATA : TEXT(10) VALUE 'RAGHU',
       LEN TYPE I,
       POS TYPE I,
       LNG TYPE I,
       VCH(10).
LEN = STRLEN( TEXT ).
LNG = LEN * 2.
LNG = LNG - 1.
DO LNG TIMES.
IF SY-INDEX LE LEN.
POS = POS + 1.
ELSE.
POS = POS - 1.
ENDIF.
VCH = TEXT+0(POS).
WRITE : / VCH.
ENDDO.

REGARDS

SHIBA DUTTA

Read only

0 Likes
897

Hi,

Really the code you given works fine.And I felt that the code given by you is good compared to others.

Yesterday I tried that code and understand myself.Its really coooooooooooool.

Once again thanks for answering me with optimized code.

Read only

Former Member
0 Likes
897

DATA: s TYPE string VALUE 'RAGHU',

int TYPE i ,

count type i.

int = STRLEN( s ) .

DO int TIMES.

count = sy-index.

WRITE:/ s(count).

ENDDO.

DO int TIMES.

count = int - sy-index.

WRITE:/ s(count).

ENDDO.

Rewards if useful...........

Minal

Read only

Former Member
0 Likes
897

Hi,

Thanks for replying me.Can you explain me the logic inside the Do Loop?

Can you explain me theoretically for below part of the code?What SY-IDNEX will returns?

IF SY-INDEX LE LEN.

POS = POS + 1.

ELSE.

POS = POS - 1.

ENDIF.

VCH = TEXT+0(POS).

Read only

0 Likes
897

Hii

data str type string.

data: len type i,

I TYPE I.

str = 'RAGHU'.

len = strlen( str ).

I = 1.

do len times.

write :/1(I) str.

I = I + 1.

enddo.

I = LEN.

do len times.

write :/1(I) str.

I = I - 1.

enddo.

Read only

0 Likes
897

HI,

sy-index will give u the iteration number.

first of all in this program u are counting the length of the string and it was in variable LEN.

we are always printing the string from 0 index(1st character) to POS number of characters.

length is 5 so LNG = 5 * 2 = 10 and LNG = 10 - 1 = 9.and first sy-index = 1.so,1 <= 5 so POS = 0 + 1 = 1. it is printing the first char

second sy-index = 2.so,2 <= 5 so POS = 1 + 1 = 2. it is printing the first two chars

third sy-index = 3.so,3 <= 5 so POS = 2 + 1 = 3. it is printing the first three chars

forth sy-index = 4.so,4 <= 5 so POS = 3 + 1 = 4. it is printing the first four chars

fifth sy-index = 5.so,5 <= 5 so POS = 4 + 1 = 5. it is printing the first five chars

sixth sy-index = 6.so,6 > 5 so POS = 5 - 1 = 4. it is printing the first four chars

seventh sy-index = 7.so,7 > 5 so POS = 4 - 1 = 3. it is printing the first three chars

eighth sy-index = 8.so,8 > 5 so POS = 3 - 1 = 2. it is printing the first two chars

ninth sy-index = 9.so,9 > 5 so POS = 2 - 1 = 1. it is printing the first char

rgds,

bharat.

Read only

0 Likes
897

in do loop sy-index is giving the counter of the loop . i.e. how much time it processes the loop.

so from first to the last of the string it should increase 1 character printing in each loop pass.

But when it reaches last of the string it should decrease 1 character from the printing.

so by i am adding one upto when the loop pass is reaching to the last. Then

I am subtracting from the total length.

regards

shiba dutta

Read only

Former Member
0 Likes
897

suppose in ur selection screen the field is :

S_name:raghu

code :

data : v_char type string.

v_char = s_name+0(1).Then v_char will have R

v_char = s_name+0(2).Then v_char will have RA

v_charg = s_name+0(3).Then v_char will have RAG.

LIKE THIS YOU HAVE TO PROCEED.

this CODING HELPS YOU. Give marks if it helps