Skandix Skandix - 7 months ago 7
Vb.net Question

VB2010 String adds up Length by adding ""

I'am comparing

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

Example:

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


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

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


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
indexOf()
to compare something, I
Replace("§", "")
it.

(with
Replace()
it is deleted)

Answer

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(""))