Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation of two strings does not work [duplicate]

I have the following code, but it doesn't work:

 CHARACTER*260 xx, yy, zz     
  xx = 'A'   
  yy = 'B'
  zz = xx // yy

When I debug my code in Visual Studio the

  • variable xx contains 'A'
  • variable yy contains 'B'
  • variable zz contains 'A'

Why doesn't zz contain 'AB'?

like image 419
user3443063 Avatar asked Jun 30 '15 09:06

user3443063


People also ask

Can you use a double in string concatenation?

The “+” operator with a String acts as a concatenation operator. Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object. In-fact adding a double value to String is the easiest way to convert a double value to Strings.

What happens when concatenate two strings?

In Java, String concatenation forms a new String that is the combination of multiple strings.

Is concatenation only for strings?

string concat() method takes only string arguments, if there is any other type is given in arguments then it will raise an error.

Can you concatenate a double to a string in Java?

Using the “+” operator − It concatenates the other operand to String and returns a String object. You can convert a double value into a String by simply adding it to an empty String using the “+” operator. In-fact it is the easy way to convert a double value to a String.


1 Answers

You defined xx to be 260 characters long. Assigning a shorter character literal will result in a padding with blanks. Thus, xx contains A and 259 blanks. yy contains B and 259 blanks. So the concatenated string would be 'A' + 259 blanks + 'B' + 259 blanks, in total 520 characters.

Since zz is only 260 characters long, the rest is cropped.

What you are trying to do is achieved by

zz = trim(xx) // trim(yy)

trim() removes trailing whitespace from strings.

like image 60
Alexander Vogt Avatar answered Oct 23 '22 16:10

Alexander Vogt