tags:

views:

1344

answers:

10

Here is the sample:

    Dim TestString As String = "Hello," & Chr(0) & "World"
    MsgBox(TestString, , "TestString.Length=" & TestString.Length.ToString)

Result - Messagebox shows "Hello," with title says TestString.Length=12

I guess the chr(0) is treated as the end of zero terminated string, but it's not what i want.

What the right method to operate with Chr(0) ?

A: 

chr(0) is null. Are you after a single space? chr(32)?

Nick DeVore
Just to be petulant: chr(0) is NUL. NUL and null are quite different concepts even if they both evaluate to zero.
James Curran
Huh - you learn something new every day. Thanks for the heads up!
Nick DeVore
A: 

Indeed Chr(0) is used to terminate a string, if you want a newline character you can use Environment.NewLine() in VB.NET or vbNewLine in VB6

Alonso
A: 

You are absolutely right that Chr(0) is treated as a control character that means "this is the end of this string." How would you prefer this character be represented? A space? A newline? Do you only need to print the string in its entirety, or are there other things about the string that are significant?

I'd just call TestString.Replace(chr(0), " ") or whatever the equivalent is in VB.NET

Ryan
A: 

I need the chr(0) because it's part of the security of the people that I'm sending it to. This was a VB6 component that I'm moving over to .net. I can't use " " because they use the chr(0) as part of their security algorithm. Trust me I wish I could a blank space. Would make my life easier. lol

Alfonso Pajares
It would be best if you added this (and any further answers you give to your question) directly to the question, so anyone looking can directly see all the relevant info.
Adriano Varoli Piazza
A: 

I don't think the problem is with the vb string as evidenced by your length output. I think it's with the MsgBox function that probably still uses the windows api that treats char(0) as a terminator.

If your debugging or something and you want to represent that there is a char(0) you should replace it with a printable set of characters that you understand represents the char(0).

CrashCodes
A: 

Seems like VB is handling the string correctly because it knows the correct length (12), so the problem is just when you display the string. Can you replace chr(0) with a space just when you want to display it?

jeffm
A: 

I've also tried this in asp.net with no avail. I'm sending the string over a socket. My problem is that when I'm debugging it the whole string doesn't add up. I need to send them a rather large string for it. What comes up really on the message window doesn't bother me. I used the above as an example. I just need when putting the multiple variables that it adds correctly to the string. Another example of it would be

"SCORE".ToString.PadRight(8, chr(0)) & "3.0".ToString.PadRight(10, chr(0)) & "0".ToString.PadRight(1, "0")

the expected value would score3.00

What I get though is score3.0

Alfonso Pajares
This works fine for me in a console application -- are you sure that your output string is long enough to include all the padding?
jeffm
Note also that the debugger (at least in VS2005) does *not* show the string properly but it outputs correctly to the console.
jeffm
In the real application I'm putting together like 30 properties. The only two that it respects though are the first two properties and doesn't add the rest. If I put in the immediate window the rest of the properties that don't have chr(0) it adds up just fine. The first two are the ones with chr(0)
Alfonso Pajares
A: 

MsgBox most likly uses the windows API function MessageBox, which uses a null terminated string, and thus will only display up to the first null. Most of the windows api works(if not all?) with null terminated string so you may have to find a diffrent way to display the string.

Your debugging tools (eg visual studio) may also treat strings as null terminated, which again will only display up to the first null (however I'm not sure on this, and am not currently at a computer I can test it on for vs08).

Fire Lancer
A: 

The sample code in the questioner's example ("SCORE".ToString...) works fine for me in a console application. The VS2005 debugger does not show the string correctly, but it outputs to the console just fine. So, my feeling is either that you think it's incorrect because the debugger wrongly says so or your output string is not long enough to include all the padding.

jeffm
A: 

Take a look at Jon Skeet's article in regard to strings. It appears that the TestString would contains all of the characters, including the embeded \0, but it is not displayed correclty. The article includes a function (in c#) that displays the string properly.

pro3carp3