C# string won't concatenate?

I believe the problem is related to not keeping track of the total number of bytes read. Your byte buffer, set to ReceiveBufferSize, is more than likely larger than the actual number of bytes read. By taking into account the actual bytes read, and then passing it to the Encoding.

UTF8.GetString() method, you should get a valid string that can be concatenated. Here's an example.

Up vote 0 down vote favorite share g+ share fb share tw.

Reads NetworkStream into a byte buffer. NetworkStream ns; System.Net.Sockets. TcpClient client = new TcpClient(); byte receiveBytes = new byteclient.

ReceiveBufferSize; ns. Read(receiveBytes, 0, (int)client. ReceiveBufferSize); String returndata = Encoding.

UTF8. GetString(receiveBytes); I am successfully reading from a client and storing the result into a string called returndata. However, when I try to concatenate returndata with anything, no concatenation occurs.

Ex: String. Concat(returndata, "test") returns returndata, as does returndata + "test". Does anyone know why this is happening?

Edit: Steve W is correct; I found out later that returndata. Length was always returning 8192. C# string concatenation link|improve this question edited Feb 13 '09 at 20:40 asked Feb 13 '09 at 19:54user662221216.

Would be better if you showed the actual code you are having trouble with. You've posted the code you've got working and neglected the code you haven't. – AnthonyWJones Feb 13 '09 at 19:59 Can you include the code for the concatenation?

– Zach Scrivena Feb 13 '09 at 19:59 @tacogordito: Did you check the return value of ns.Read()? – Zach Scrivena Feb 13 '09 at 20:02 Agree with Anthony and Zach: why would you leave out the code that is causing the problems! – abelenky Feb 13 '09 at 20:13.

I believe the problem is related to not keeping track of the total number of bytes read. Your byte buffer, set to ReceiveBufferSize, is more than likely larger than the actual number of bytes read. By taking into account the actual bytes read, and then passing it to the Encoding.

UTF8.GetString() method, you should get a valid string that can be concatenated. Here's an example: NetworkStream ns; System.Net.Sockets. TcpClient client = new TcpClient(); byte receiveBytes = new byteclient.

ReceiveBufferSize; int bytesRead = ns. Read(receiveBytes, 0, receiveBytes. Length); String returndata = Encoding.

UTF8. GetString(receiveBytes,0,bytesRead); returndata = returndata. Trim(new char {'\0'}); Note also the other suggestion about reading a null terminator and including it in the bytes read also could be an issue.

I've included a Trim of the string to make sure null terminators are removed.

Returndata = string. Concat(returndata, "test"); returndata += "test.

Psychic debugging - gotta love it :) – ShuggyCoUk Feb 13 '09 at 19:59.

To expand on jhunter's answer, the Concat method doesn't alter the contents of the original string variable, it just returns the concatenated result. If you want returndata to contain the result of the concatenation, you need to use returndata = string. Concat(returndata, "test").

Its not just the Concat method, NO method directly modifies strings in C#. Every string is a unique, immutable object. If you change a string, it makes a new copy.

– abelenky Feb 13 '09 at 20:12 That's true--bad choice of words on my part. – Mike Powell Feb 13 '09 at 20:19.

I'll have a guess: The received string includes a '\0' terminator. You ought to be able to check that with the debugger.

Excellent suggestion, considering the context. +1 – Mike Powell Feb 13 '09 at 20:00 I put my bets on this, too. – Timbo Feb 13 '09 at 20:02 Wrong.

Only C strings are null terminated. – Tanveer Badar Feb 13 '09 at 20:53 @Tanveer: Yes, so what if the sender is a C/C++ program, that includes the '\0'? .

NET doesn't need the EOS char but can handle it - with results like tacogordito describes. – Henk Holterman Feb 13 '09 at 21:14 .net strings actually include a nul terminator, it's just that (by virtue of the length value) it can also include nulls as well (though it will no longer function as expected when passed to old style C functions via marshalling) – ShuggyCoUk Feb 13 '09 at 23:01.

Strings are immutable. This means that, when you add (concatenate) another string to your string, you'll receive a new string instance. The original string itself won't be changed.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions