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: 

Split string

Former Member
0 Kudos

Hello

I have a variable string which has the value:

'string1;string2;string3;string4;string5'

I want to use each individual string from the list.

i.e. loop through all the strings[n] and process them.

What are your views on the best way to extract these string?

Can I split them into a table and loop through the table? What would be the syntax for this?

I am new to ABAP and interested in the best way to carry out processing.

Cheers

Andrew

1 ACCEPTED SOLUTION

former_member189059
Active Contributor
0 Kudos

split it once.. check if the first of the new strings is the same as the original string

if it is different, then split it again till it becomes the same

run it in a loop

6 REPLIES 6

Former Member
0 Kudos

Hi,

do like this.

DATA: string(60) TYPE c value 'string1;string2;string3;string4;string5',

p1(20),

p2(20),

p3(20),

p4(20),

P5(20),

del VALUE ';'.

WRITE string.

SPLIT string AT del INTO p1 p2 p3 p4 p5.

WRITE / p1.

WRITE / p2.

WRITE / p3.

WRITE / p4.

WRITE / p5.

rgds,

bharat.

Message was edited by:

Bharat Kalagara

former_member189059
Active Contributor
0 Kudos

try this

split mainstring at ';' into string1 string2 string3 string4 string5.

Former Member
0 Kudos

Hey guys thanks for your answers

I forgot to mention that the number of strings is not set. The number changes. So I need smething a bit more flexible than the static solution.

Cheers

Andrew

0 Kudos

when the no. of occurrences of the string is unknow the , best, simple approach would be to use the following method.

data: string_tab type standard table of string .

data: temp_string type string .

refres: string_tab .

split mainstring at ';' into table string_tab .

loop at string_tab into temp_string .

  • do some processing of each split string .

clear temp_string .

endloop .

Former Member
0 Kudos

Hi,

DATA: string(400) TYPE c VALUE 'abac;abdfa;adfa;adsf'. " this is your string.

DATA: char1(400) TYPE c,

char2(400) TYPE c.

DATA: string1(400) TYPE c.

DATA: y TYPE i.

DATA: num TYPE i.

num = STRLEN( string ).

char1 = string.

string1 = string.

DO num TIMES.

SEARCH string FOR ';' .

IF sy-subrc EQ 0.

SPLIT string AT ';' INTO char1 char2.

y = y + 1.

CLEAR: string.

string = char2.

ENDIF.

ENDDO.

data: begin of itab occurs 0,

field(400) type c,

end of itab.

CLEAR: char1 , char2.

DO y TIMES.

SPLIT string1 AT ';' INTO char1 char2.

itab-field = char1.

write:/ itab-field.

append itab.

clear: string1.

string1 = char2.

ENDDO.

thanks & regards,

Venkatesh

former_member189059
Active Contributor
0 Kudos

split it once.. check if the first of the new strings is the same as the original string

if it is different, then split it again till it becomes the same

run it in a loop