04-18-2006 2:05 PM
Though in ABAP world for a long time...I've always struggled with simple string conversions like this one.
I'm writing an ABAP object that retrieves dfies data of a structure and based on the length of each field, I concatenate the overall values appropriately separated by blanks.
Ex:
Struct1 has
fld1 length 10 "ABAP"
fld2 length 7 "field"
fld3 length 5 "conv"
My output should go into newStr type string this way
"ABAP field conv "
Please let me know the easiest and efficient string functions I can use to get this. Somehow shift left by n places, did not work.
thanks.
04-18-2006 2:08 PM
Hi,
concatenate fld1 fld2 fld3 into newstr separated by space.
Andreas
04-18-2006 2:09 PM
CONCATENATE fld1 fld2 fld3 into string separated by SPACE.
Regards,
Ravi
Please mark the helpful answers
04-18-2006 2:09 PM
concatenate fld1 fld2 fld3 into newstr separated by space
Message was edited by: Sekhar
04-18-2006 2:10 PM
CONCATENATE fld1 fld2 fld3 INTO lc_char SEPARATED BY space.
ls_string = lc_char.
04-18-2006 2:13 PM
REPORT ZTEST_STRING .
data: a(10) type c,
b(7),
c(5),
d(22).
a = 'ABAP'.
b = 'field'.
c = 'conv'.
concatenate a b c into d separated by space.
write d.
04-18-2006 2:14 PM
All,
Concatenate with 'SEPARATED BY space.' gives only single blank. I need exact number of blanks as defined in the structure for each field.
Thanks.
04-18-2006 2:15 PM
By the way, after I posted my message, I see that xml content of the screen dropped all the additional blanks I had....each field should be separated by right number of blanks as defined in the field length.
04-18-2006 2:25 PM
ok,
2nd try:
put your fields in an int.table / structure
DO .
ASSIGN COMPONENT sy-index OF STRUCTURE wa TO <fs1>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
len = streln( <fs1> ).
offs = offs + len + 1.
move <fs1> to nwestr+offs.
ENDDO.
04-18-2006 2:20 PM
REPORT ychatest.
data : str1(10),
str2(7),
str3(5),
str(22).
str1 = 'ABAP'.
str2 = 'Field'.
str3 = 'Conv'.
str+0(10) = str1.
str+10(7) = str2.
str+17(5) = str3.
write : str.
04-18-2006 2:21 PM
I'll try this and will get back to you. Thanks for a quick reply. All my field lengths are variables...let me work on it.
04-18-2006 2:27 PM
My code now looks like this
newStr type string.
<b>newStr+dfies-offset(dfies-leng) = fld.</b>
where fld takes values from fld1, fld2, fld3, etc.
I get this message when I active -
At the write position, you cannot use offset and length with fields of type "STRING" or "XSTRING".
seems like I cannot use string type for this? I do need string type since I have large message.
Any ideas?
04-18-2006 2:35 PM
hi,
try that 3.:
DATA str(10000).
DATA newstr TYPE string.
DATA: BEGIN OF wa,
fld1(10) VALUE 'ABAP',
fld2(7) VALUE 'field',
fld3(5) VALUE 'conv',
END OF wa.
FIELD-SYMBOLS <fs1>.
DATA len TYPE i.
DATA offs TYPE i.
DO .
ASSIGN COMPONENT sy-index OF STRUCTURE wa TO <fs1>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
DESCRIBE FIELD <fs1> LENGTH len.
MOVE <fs1> TO str+offs(len).
offs = offs + len + 1.
ENDDO.
newstr = str.
Andreas
Message was edited by: Andreas Mann
04-18-2006 2:56 PM
Hi Andreas / Sekhar,
both your solutions work....though Andreas is a little more flexible. However, I'm still not sure if I can do this with character type and than assign it to string type. I'm dealing with huge xml files and I read fields from by tag lookup and than I copy this to a data string of type string. I hate to toggle between string and character types.
How big can character field be?
thanks.
04-18-2006 3:06 PM
instead of declaring newStr as type string declare it as char of lenght 1000 , that error will not come
04-18-2006 3:08 PM
For type "C", a maximum length specification of 65535 is allowed.
04-18-2006 3:13 PM
don't think 1000 bytes will cut it....how big can character field be. Thanks.
04-18-2006 3:19 PM
here the allowed lenght:
Type allowed length
C 1 - 65535
N 1 - 65535
P 1 - 16
X 1 - 65535
Andreas
04-18-2006 3:28 PM
If character field can accomadate a max of 65K than I'll have problems....my string can be as big as 2M.
That brings me back to my original issue.
I have an idea...I can use
<b>replace section offset dfies-offset length dfies-leng of newStr with field</b>
where field take different values in a loop like fld1, fld2, fld3, etc.
To do this, I need to initialize a string of a particular size with blanks.
data: newStr type string.
If I know the strlen of newStr ex: say 70000....how can I initialize it with blanks....any idea.
Thanks.