Skandix Skandix - 1 year ago 38 Question

VB2010 String adds up Length by adding ""

I'am comparing

in Visual Basic 2010 Express. While cuting the
together it sometimes adds a
, what I hoped is "nothing"


Dim text as String = "test"
Dim sign as Char = ""
text = text + sign

while debuging it says that the new text is
, but if I ask for the
it is 5.

This is a problem when I try to compare this with an other

Dim bigtext as String = "test1234"
Dim text as String = "test"
Dim sign as Char = ""
text = text + sign
bigtext.indexOf(text) 'should be 0 (index), but is -1 (not found)

any idea how to filter a
away or any other workaround?

Edit - my workoround for now:

Now I add
everywhere instead of
and when I need to use
to compare something, I
Replace("§", "")

it is deleted)

Answer Source

As far as I can see, a Char variable always has a character in it (which can be the null character). Concatenating it to another string will append that character to the existing string.

I see two workarounds:

  1. Use a String for sign instead of a Char. The string could be empty or have a single character in it.

  2. Trim the undesired character from the resulting string:

    text = (text + sign).Trim(CChar(""))