cant concat more than 2 strings

  • Thread starter Thread starter Nikhil Patel
  • Start date Start date
N

Nikhil Patel

Hi,
I have following line in my code.

string strDDEExpression="[Replace(" + strWorkArea + ",";

When I run this and check the value in strDDEExpression, I find that the
comma is missing.

Any idea?

Thanks...

-Nikhil
 
Nikhil Patel said:
Hi,
I have following line in my code.

string strDDEExpression="[Replace(" + strWorkArea + ",";

When I run this and check the value in strDDEExpression, I find that the
comma is missing.

Any idea?

I think you have a trailing char(0) or null terminator in strWorkArea.

eg

char c = System.Text.Encoding.ASCII.GetChars( new byte[] {0})[0];
string strWorkArea = new string(new char[] {'h','e','l','l','o', c});
string strDDEExpression="[Replace(" + strWorkArea + ",";
Console.WriteLine(strDDEExpression);

outputs

[Replace(hello

You need to get rid of the null terminator.


David
 
Ok.
now I get it. The string in C# works in C++ way. I am a VB programmer.
So forgot about null terminator. But I am wondering even in C++, when you
use "+" to concat strings, doesn't it get rid of the null terminators of all
strings except the last one?

Thanks David.

David Browne said:
Nikhil Patel said:
Hi,
I have following line in my code.

string strDDEExpression="[Replace(" + strWorkArea + ",";

When I run this and check the value in strDDEExpression, I find that the
comma is missing.

Any idea?

I think you have a trailing char(0) or null terminator in strWorkArea.

eg

char c = System.Text.Encoding.ASCII.GetChars( new byte[] {0})[0];
string strWorkArea = new string(new char[] {'h','e','l','l','o', c});
string strDDEExpression="[Replace(" + strWorkArea + ",";
Console.WriteLine(strDDEExpression);

outputs

[Replace(hello

You need to get rid of the null terminator.


David
 
Nikhil Patel said:
Ok.
now I get it. The string in C# works in C++ way. I am a VB programmer.
So forgot about null terminator. But I am wondering even in C++, when you
use "+" to concat strings, doesn't it get rid of the null terminators of all
strings except the last one?

No a string in C# is exactly like a string in VB. Internally it is a fixed
length array of chars.
There is no need for a null terminator, since the array has a length.

string's in C# are NOT null termintated. You should never have a null char
in a string in C# or VB, and it's actually pretty hard to get one in there.

If you do manage to get a null char into a string, some things don't work
quite right.

It's just the only thing I could think of which would cause your
concatenation to fail.

David
 
Hi,
I checked the value in strWorkArea. It is "86463496" and its length
property is 8. So I don't think there is any hidden character.

David Browne said:
Nikhil Patel said:
Hi,
I have following line in my code.

string strDDEExpression="[Replace(" + strWorkArea + ",";

When I run this and check the value in strDDEExpression, I find that the
comma is missing.

Any idea?

I think you have a trailing char(0) or null terminator in strWorkArea.

eg

char c = System.Text.Encoding.ASCII.GetChars( new byte[] {0})[0];
string strWorkArea = new string(new char[] {'h','e','l','l','o', c});
string strDDEExpression="[Replace(" + strWorkArea + ",";
Console.WriteLine(strDDEExpression);

outputs

[Replace(hello

You need to get rid of the null terminator.


David
 
Can you create a small repro case? Could you send your compiled or
dissassembled (ILDASM) code too? What version of the framework are
you using?
-mike
MVP

Nikhil Patel said:
Hi,
I checked the value in strWorkArea. It is "86463496" and its length
property is 8. So I don't think there is any hidden character.

message news:#[email protected]...
Nikhil Patel said:
Hi,
I have following line in my code.

string strDDEExpression="[Replace(" + strWorkArea + ",";

When I run this and check the value in strDDEExpression, I find that the
comma is missing.

Any idea?

I think you have a trailing char(0) or null terminator in strWorkArea.

eg

char c = System.Text.Encoding.ASCII.GetChars( new byte[] {0})[0];
string strWorkArea = new string(new char[] {'h','e','l','l','o', c});
string strDDEExpression="[Replace(" + strWorkArea + ",";
Console.WriteLine(strDDEExpression);

outputs

[Replace(hello

You need to get rid of the null terminator.


David
 
Thanks all.

This is what I did and it works.
string strDDEExpression="[Replace(" +
strWorkArea.Remove(strWorkArea.Length-1,1) + ",";
 
David,
string's in C# are NOT null termintated. You should never have a null char
in a string in C# or VB, and it's actually pretty hard to get one in
there.
You do need null char in C#, VB6, & VB.NET strings when you are calling
Win32 APIs. As some Win32 APIs use a single null char as a seperator and
double null chars as a terminator.

Also, some external communication (TCP/IP, comm ports) may need null chars
in the middle of the string being transmitted...

It is actually rather nice that .NET Strings fully support null chars, as a
null char is still a valid character. Its just some of the interop stuff
chokes (debugger, System.Console, System.Diagnostics.Debug,
System.Diagnostics.Trace) when displayed in VS.NET especially.

Just a thought
Jay
 
Back
Top