# Bug in String concatenation

**URL:** https://talk.remobjects.com/t/bug-in-string-concatenation/25653
**Category:** Mercury
**Created:** [November 4, 2021, 6:04pm UTC](https://talk.remobjects.com/t/bug-in-string-concatenation/25653 "2021-11-04T18:04:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Theo69](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@Theo69](https://talk.remobjects.com/u/Theo69)
#### Post date: [November 4, 2021, 6:04pm UTC](https://talk.remobjects.com/t/bug-in-string-concatenation/25653/1 "2021-11-04T18:04:55Z")

</div>

I have the following code:

```
Function NormalStringModification(s As String) As String
  Dim rs As String = ""
  For i = 0 To s.Length - 4 Step 5
    rs &= s.Substring(0, i) & "X"
  Next
  Return rs
End Function

```

When I compile this, and decompile to VB again, I see:

 ![image](https://talk.remobjects.com/uploads/default/original/3X/3/4/34a4caf4d50e7ea9be299ab8311820393f3b2fb3.png)

The check on startindex and lenght is not needed as this is already done in the Substring routine of String. The nothing (or Null) check is just plain wrong. When a string is nothing or Null in VB and you concatenate it, it is seen as an empty (“”) string.  
But also here, the needed checks are already done in the String.Concat method.

So, both checks are not needed, and the code can be compiled to (as it is by VB):

 ![image](https://talk.remobjects.com/uploads/default/original/3X/7/6/768b2fac7cebdaf04637f23737598ba46c761f47.png)

Or, in IL:

 ![image](https://talk.remobjects.com/uploads/default/original/3X/6/f/6febbadc3f9aecd79c5f5b27e226c9265918f442.png)

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [November 4, 2021, 6:33pm UTC](https://talk.remobjects.com/t/bug-in-string-concatenation/25653/2 "2021-11-04T18:33:43Z")

</div>

Logged as [bugs://E25493](https://www.remobjects.com/portal/bugs/?id=E25493).

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [November 4, 2021, 6:34pm UTC](https://talk.remobjects.com/t/bug-in-string-concatenation/25653/3 "2021-11-04T18:34:06Z")

</div>

this is not _broken_, only an optimization, correct?

---

<div class="post-metadata">

### Author: ![Theo69](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@Theo69](https://talk.remobjects.com/u/Theo69)
#### Post date: [November 4, 2021, 6:35pm UTC](https://talk.remobjects.com/t/bug-in-string-concatenation/25653/4 "2021-11-04T18:35:05Z")

</div>

The null check is a breaking change.
