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

Obtaining substring

Former Member
0 Likes
1,327

I think I posted this question somewhere before, but I am really confused and could no longer find the original post, so sorry guys if you have to spend your time respose this again:

How can I pull out a sub-string from a string that is delimited by something without using an internal table (i.e., use SPLIT), say for example I would like to have

str1 str2 str3 str4

which are tab-delimited to be separated so that i can do something to str1, pull it out, come back with a substring containing str2, 3 and 4 and continue like that until I reach str4, how can I do that?

str1,2,3,4 are of varying length, BTW.

Thanks a lot!

Regards,

Anyi

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,263

You can use the split like this.



data: field1 type string.
data: field2 type string.
data: field3 type string.

split string at 
    cl_abap_char_utilities=>horizontal_tab 
            into field1 field2 field3.

Regard,

Rich Heilman

9 REPLIES 9
Read only

Former Member
0 Likes
1,263

hi

use offset

or

split lv_data CL_abap_char_utilities=>horizontal_tab

str1 str2 str3 str4.

Message was edited by: Manoj Gupta

Read only

0 Likes
1,263

Err..the problem is, if I do not know the length of str1, 2, 3 and 4, how can I use offset? Thanks!

Read only

0 Likes
1,263

Hi,

Try like this.

data : TAB TYPE X VALUE '09'.

SPLIT i_tab AT tab INTO f1 f2 f3.

Laxman

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,264

You can use the split like this.



data: field1 type string.
data: field2 type string.
data: field3 type string.

split string at 
    cl_abap_char_utilities=>horizontal_tab 
            into field1 field2 field3.

Regard,

Rich Heilman

Read only

0 Likes
1,263

Err...I think I did not mentioned clearly here, but I do not know how many so-called fields are there as well, i.e., I have up to n sunstrings that are tab-delimited here, where n could be anything from 1 to 100, say. Thanks!

Regards,

Anyi

Read only

0 Likes
1,263

DATA: v_string TYPE string VALUE 'abc*def'.

SEARCH v_string FOR '*' .

or

data : v_string type string value 'Anyi',

v_lth type i,

v_res type c.

v_lth = strlen( v_string ).

if count < v_lth.

if v_res = 'n'

v_res = v_string + 0( count ).

count = count + 1.

endif.

endif.

else use search statment

or

Go through the following procedure

Word wrapping/Split

-


You can use the fucntion module SWA_STRING_SPLIT to split a tring into

smaller strings or use function module SWA_STRING_SPLIT

This example shows how to split the text in a field of one internal

table and place it into a field in another internal table that has

a shorter size

REPORT z_hfa_test .

DATA:

  • Table containing texts that has to be split

BEGIN OF it_longstrings OCCURS 0,

line TYPE string,

END OF it_longstrings,

  • Output table from function SWA_STRING_SPLIT

it_string_components LIKE swastrtab OCCURS 0 WITH HEADER LINE,

  • Table containg the resukting strings

BEGIN OF it_shortstrings OCCURS 0,

line(10) TYPE c,

END OF it_shortstrings,

start-of-selection.

  • Add some lines to it_longstrings

it_longstrings-line = '123456789012345 Hallo this is a very long'.

APPEND it_longstrings.

it_longstrings-line = 'line, that has to be split.'.

APPEND it_longstrings.

it_longstrings-line = 'The line has to be split into lines of 10

characters'.

APPEND it_longstrings.

  • Split each line of it_longstrings into lines with length 10

  • The resuklting lines are palced in table it_string_components

LOOP AT it_longstrings.

CLEAR it_string_components. REFRESH it_string_components.

CALL FUNCTION 'SWA_STRING_SPLIT'

EXPORTING

input_string = it_longstrings-line

max_component_length = 10

  • TERMINATING_SEPARATORS =

  • OPENING_SEPARATORS =

TABLES

string_components = it_string_components

  • EXCEPTIONS

  • MAX_COMPONENT_LENGTH_INVALID = 1

  • OTHERS = 2

.

IF sy-subrc <> 0.

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

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

ENDIF.

  • Add the lines to table

LOOP AT it_string_components.

it_shortstrings-line = it_string_components-str.

APPEND it_shortstrings.

ENDLOOP.

ENDLOOP.

END-OF-SELECTION.

LOOP AT it_shortstrings.

WRITE: / it_shortstrings-line.

ENDLOOP.

Splitting delimited string

-


Instead of placing the substring ind l_output, it could be palced in an

internal

table using this syntax:

SPLIT f AT g INTO TABLE itab.

Each substring will be placed in its own table row. In the example below, the table would have 5 rows.

Note that all output fields must be of type char or string

REPORT YDK8HENFR_TEST1 line-size 80.

data: l_string(132) type c.

data: begin of l_output,

name(40) type c,

age(10) type c,

address(40) type c,

city(40) type c,

zipcode(4) type c,

end of l_output.

start-of-selection.

l_string = 'John;28;My street;My city;3000'.

split l_string at ';' into l_output-name

l_output-age

l_output-address

l_output-city

l_output-zipcode.

write: / l_output-name.

write: / l_output-age.

write: / l_output-address.

write: / l_output-city.

write: / l_output-zipc.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,263

Well then the easiest way is to split it into an internal table. Is there a reason why you don't want to do that.

Regards,

Rich Heilman

Read only

0 Likes
1,263

To be honest, I was just too lazy to declare an internal table , I thought there may be some easier way of doing that, but I guess I still have to take the long way in the end...I am from a JAVA background, and in JAVA, you can just use substring to do it, I thought ABAP would be that smart as well, but apparently it is not :). Thanks a lot for all the replies!

Read only

0 Likes
1,263

<i>I thought ABAP would be that smart as well, but apparently it is not</i>

Ohhhh...... you are not talking bad about ABAP in the ABAP forum, are you?

Too lazy to declare an internal table? Its one line of code.

Data: itab type table of string with header line.

Regard,

Rich Heilman