I have a strange problem, when I try to join two strings, one declared as a string()

dim string1 as string()

the string contains some data in both str(0) and str(1)
but when I try to add that with a regular string: "some text" like this:

dim string1 as string()
dim string2 as string

string2 = string1(1) & " Some text"

let's say string1(1) = "here is"

Which should result in: string2 = "here is Some text" BUT, all I get when the code execute is "here is not even a finishing "...
So where does: some text" go?
It's kind of crucial for my application to get the whole string since it provides needed data to continue! :S

happy for any thoughts and/or solutions :)

8 Years
Discussion Span
Last Post by rapture

I would think like this, You have string 1 = string2 but string 1 = nothing

dim string1 as string()
dim string2 as string()

string2 = " Some text"
String1 = "some text"


There is "stuff" in every string, I just tried to make it simple because it is a part of a chat program.
The problem is that when the client sends something to the server, it needs to be split therefore the "dim tokens as string()"
however, when the server sends the data back to the client, and it gets split again. i.e the server sends "JOIN|user" the string gets split at "|" making the tokens(0) contain "CHAT" and tokens(1) contain "user".

So the "JOIN" command gets into a certain loop, where the tokens(1) is used to identify the new user, but when I try to get tokens(1) & "has joined the chat" I get the previously mentioned problem. It's a little complicated to explain, so I attached some code to maybe make it easier:

Dim buffer As Byte() = New Byte(2047) {}
'reads data from the server
           networkstream.Read(buffer, 0, buffer.Length)
'and converts it to a string
            Dim chatter As String = System.Text.Encoding.Unicode.GetString(buffer)
'declaring the "tokens" containing the two pieces "JOIN" and "user"
            Dim tokens As String() = chatter.Split("|")

 ' checks if the specified text in tokens(0) compares to any commands
            If tokens(0) = "JOIN" Then
'and if it does the text "user has connected" should be shown to the end user.
'But EVERYTHING after tokens(1) disappears so I'm left with only the user's name, and that won't make any sense when the end user only sees a name, and not knowing what's the meaning of it
                msg(tokens(1) & " has connected", 1)
            End If

I know this is csharp corner but the article is on vb.net


That's a very useful link and I thank you for showing me, but I couldn't see anyting solving my problem, I've even tried placing both "tokens(1)" and "Has connected" in an own array then joining them...
But I get the same result. I can place everything I want before "tokens(1)", but after that, nothing

Dim Test1 As String = "Box of "
        Dim Test2 As String = "Rabbits"
        MsgBox(Test1 + Test2)

Results in a message box that states:

Box of Rabbits

Note: I clearly was not paying full attention to the responses within this post, and this does not solve your problem. I'll just leave it here because there is no delete and who knows, maybe some mislead sole will find it later to be useful.


Thanks for trying though Minolwen :)

As you (maybe) can see in the attachment image, I AM forwarding the whole message to the msg sub, but only the tokens(1) gets through :(

Attachments string_manipulation.png 53.34 KB

Just here to tell you I've solved it! :D

Step by step from the error I went back to eventually find that the problem were between the retrival of the bytes and the conversion to string which ended in the string did not convert entirely, leaving me without the finishing ", therefore the problems, solving this also solved many of my previous problems :D I'm kinda' happy right now :P


sometimes those problems that cause you to struggle for a long time are very rewarding when you finish them yourself. good job

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.