‎2006 Jun 22 8:45 AM
Hi,
I am an absolute beginner in ABAP, maybe somebody can help me with this :
If a certain string variable has a length bigger than 60 characters, the value should be cut after 57th character and "..." added.
with regards
‎2006 Jun 22 8:55 AM
HI Erik Dierinck ,
Good
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.
-
Good Luck and reward me for the same
Thanks
Ashok.N
‎2006 Jun 22 8:49 AM
Hi Eric ,
You can use SPLIT statement to split a string at a particular location and CONCATENATE statement to concatenate this new string with the required remaining portion.
The syntax is
SPLIT <source_string> at <location> into <new_string>.
CONCATENATE <new_string> <remaining_portion> into <final_string>.
Or you can find the offset and then concatenate them..
Regards,
SP.
‎2006 Jun 22 8:52 AM
Erik,
DATA : VAR(60) type c.
Now if you want the last 3 characaters
VAR2 = VAR+57(3).
Now var2 will have 3 characters from the 58th position.
Now you can concatenate
CONCATENATE VAR2 '...' INTO VAR.
Regards,
Ravi
Note : Please mark the helpful answers and close the thread if the issue is resolved.
‎2006 Jun 22 8:54 AM
Hi,
check this code.
len = strlen( string_variable )
IF len > 57.
string_variable+57(remaing_lenght) = SPACE.
string_variable+57(4) = '....'.
ENDIF.
Regards,
Wasim Ahmed
‎2006 Jun 22 8:55 AM
HI Erik Dierinck ,
Good
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.
-
Good Luck and reward me for the same
Thanks
Ashok.N