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

Add 2 strings together

Former Member
0 Likes
5,500

Hello,

i have a really weird error message here:

say I define 2 dta type c objects:

data: tot_str(50) type c,

sub_str(20) type c.

then I do

tot_str = 'ABCDEF' & substr

assuming both tot_str and sub_str are already initialized.

However, the error message I got is:

Incorrect arithmetic or bit expression: Instead of "&", an operator (+, -, *, /, ... or BIT-AND, BIT-XOR, BIT-OR) was expected.)

Why is it so? I thought & is the standard concatenation character for ABAP. And BTW, when I changed to +, it will give me a run-time error saying that the 2 strings can not be added....

Thanks a lot!

Regards,

Anyi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,003

Hi there,

Using cancatenate str1 str2 into Tot_string.

Hope this helps

11 REPLIES 11
Read only

Former Member
0 Likes
2,004

Hi there,

Using cancatenate str1 str2 into Tot_string.

Hope this helps

Read only

Manohar2u
Active Contributor
0 Likes
2,003

wa_test(100) type c.

concatenate tot_str sub_str into wa_test.

Regds

Manohar

Read only

Former Member
0 Likes
2,003

CONCATENATE 'ABCDEF' SUBSTR INTO TOT_STR.

the above should resolve your issue.

U can surely use SEPARATED BY SPACE or any other delimiter.

Regards

Anurag

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,003

Anyi, you can use the & symbol only when working with literals.

For example.

str = 'ABCDEF' & 'GHIJKL'.

When using variables, you should use CONCATENATE. The + sign will not help you as it does in java.

concatenate 'ABCDEF' substr into tot_str.

REgards,

Rich Heilman

Read only

Former Member
0 Likes
2,003

HI

good

data: tot_str(50) type c,

sub_str(20) type c.

then I do

tot_str = 'ABCDEF' & substr

this is your codes

-


ANSWER

data: tot_str(50) type c,

sub_str(20) type c,

XYZ(20) TYPE C VALUE 'ABCDEF'.

ONCATENATE XYZ sub_str INTO TOT_STR.

thanks

mrutyun

Read only

Former Member
0 Likes
2,003

hi,

try this simple one,

data: first(10),middle,last(10),name(21).

write:/41 first color 7,middle color 7,last color 7.

concatenate first middle last into name separated by space.

write:/41 first color 6,middle color 6,last color 6.

best of luck,

regards,

kcc

Read only

Clemenss
Active Contributor
0 Likes
2,003

Hi Anyi,

you try to use & as an operator for concatenation of strings.

Unfortunately, & and + are not defined as operators for character type variables. & is not defined as an operator in ABAP. AFAIK & is not used in ABAP for anything.

As the other guys recommended, you have to use concatenate for this purpose. But note that concatenate will remove leading and trailing blanks from the strings concatenated.

If you need the blanks, then use offsets, i.e.

tot_str = 'ABCDEF'.

tot_str+6 = substr.

Best regards,

Clemens

Read only

0 Likes
2,003

<i>Unfortunately, & and + are not defined as operators for character type variables. & is not defined as an operator in ABAP. AFAIK & is not used in ABAP for anything.</i>

& is used in ABAP. It is used to put two or more literals into a string.

report zrich_0001.

data: str type string.


str = 'This is one part of the string' &
      ' This is another part of the string' &
      ' And here is another part of the string'.

      write:/ str.

Regards,

Rich Heilman

Read only

Clemenss
Active Contributor
0 Likes
2,003

OOPS!

Sorry, Rich, you are right and I learned something.

I searched all SAP Help material available online but could not find any reference to operator &.

This is a good replacement for concatenate as it does not remove leading and trailing blanks.

Thanks a lot!

Since when do they have & in ABAP?

Regards,

Clemens Li

P.S.: I just saw that it's for literals only - so not much more than useless

Message was edited by: Clemens Li

Read only

Former Member
0 Likes
2,003

Thanks everyone for their replies.

However, I was kind of confused with Clemen's post, so if I am correct here, it means that the & is used to concatenate to string literals, i.e.,

'abcd'&'efgh' is acceptable in ABAP, and if my understanding is correct, this & is already out-dated and no longer used.

but concatenate is used in concatention of strings.

say for example

concatenate 'abcd' str1 str2.

However, the problem with concatenate is that it will remove all the leading blank spaces.

Is my understanding correct?

Thanks a lot guys!

Regards,

Anyi

> <i>Unfortunately, & and + are not defined as

> operators for character type variables. & is not

> defined as an operator in ABAP. AFAIK & is not used

> in ABAP for anything.</i>

>

> & is used in ABAP. It is used to put two or more

> literals into a string.

>

>

>

>

>

>

report zrich_0001.
> 
> data: str type string.
> 
> 
> str = 'This is one part of the string' &
>       ' This is another part of the string' &
>       ' And here is another part of the string'.
> 
>       write:/ str.

>

>

> Regards,

> Rich Heilman

Read only

Clemenss
Active Contributor
0 Likes
2,003

Hi Anyi,

yes you are right. I found it in SAP help hidden somewhere in the data objects section:

"If you want to enter a character literal in the ABAP Editor that is longer than a single editor line, ABAP syntax allows you to enter several character literals and link them using the & character. "

So this was just a crutch to bypass the (meanwhile obsolete) editor line length limit.

I the section "Processing Character Strings", chapter "Concatenating Character Strings", they say:

"The CONCATENATE statement combines two or more separate strings into one.

CONCATENATE <c1> ... <cn> INTO <c> [SEPARATED BY <s>].

This statement concatenates the character fields <c1> to <cn> and assigns the result to <c>. The system ignores spaces at the end of the individual source strings.

The addition SEPARATED BY <s> allows you to specify a character field <s> which is placed in its defined length between the individual fields."

That means that all leading and trailing spaces will disappear except the ones in the separator string <sep>.

So it depends on the result you want as how to do the concatenation. if you want a fixed position regardless of blanks, then you must work with offset and length addition.

Kind regards,

Clemens