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: 

Padding blanks to a character field and concatenating

Former Member
0 Kudos

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.

18 REPLIES 18

andreas_mann3
Active Contributor
0 Kudos

Hi,

concatenate fld1 fld2 fld3 into newstr separated by space.

Andreas

Former Member
0 Kudos

CONCATENATE fld1 fld2 fld3 into string separated by SPACE.

Regards,

Ravi

Please mark the helpful answers

Former Member
0 Kudos

concatenate fld1 fld2 fld3 into newstr separated by space

Message was edited by: Sekhar

LucianoBentiveg
Active Contributor
0 Kudos

CONCATENATE fld1 fld2 fld3 INTO lc_char SEPARATED BY space.

ls_string = lc_char.

0 Kudos
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.

0 Kudos

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.

0 Kudos

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.

0 Kudos

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.

Former Member
0 Kudos
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.

0 Kudos

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.

0 Kudos

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?

0 Kudos

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

0 Kudos

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.

0 Kudos

instead of declaring newStr as type string declare it as char of lenght 1000 , that error will not come

0 Kudos

For type "C", a maximum length specification of 65535 is allowed.

0 Kudos

don't think 1000 bytes will cut it....how big can character field be. Thanks.

0 Kudos

here the allowed lenght:

Type  allowed length
C     1 - 65535 
N     1 - 65535 
P     1 - 16 
X     1 - 65535

Andreas

0 Kudos

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.