How to specify zero termination?

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Lets say I was given a Char array as:
Dim Arr(size) As Char

and this array holds a string which ends with a C-Style zero termination.
I want to make this into a String object and cut it off at the end.

So in C# I could do:

String str = (new String(arr)).Trim('\0');

However in VB '\0' is not understood.

Please advice.

-- John
 
John,
'\0' is a char literal in C#, VB.NET uses "x"c for char literals,
unfortunately VB.NET does not have any escaped characters.

However! VB.NET does have the ControlChars class that lists a number of
common escaped characters, such as ControlChars.NullChar, alternatively you
can simply use Chr(0) or ChrW(0).

So in VB.NET you can use either:

Dim str As String = New String(arr).Trim(ControlChars.NullChar)

Or I would consider:

Dim length As Integer = Array.IndexOf(arr, ControlChars.NullChar)
Dim str = New String(arr, 0, length)

Depending on the expected size of the strings the second might be better in
that it does not allocate 2 string objects, causing extra work for the GC.
For short strings I suspect the difference would be immaterial... Based on
extensive profiling I would pick one over the other...

Hope this helps
Jay
 
Jay,
'\0' is a char literal in C#, VB.NET uses "x"c for char literals,
unfortunately VB.NET does not have any escaped characters.
Just my opinion.

fortunately VBNet ...................................

I like more simple the values. When this kind of memonics are in programs I
am always hours busy finding the right tables. However that you know already
because this is something the same as using Regex.


(Just to show that it not have to be directly an omission by mistake).

:-)

Cor
 
Cor,
Huh?

I'm sorry but your statement makes NO sense! Can you explain what you are
attempting to say?

Thanks
Jay
 
Jay,

You say, "unfortunately" VB.NET does not have any escaped characters

Others like me say "fortunately".

I have by the way thought about this later when I had already written this
message. A long time ago I abanded books and started more too rely on
trustable Interenet sites. That is as we have seen a difference in approach
between us.

Things as those charachters are easy to find in books, while on Internet
that is often a hell.

It is just that we have another opinion, where I respect yours of course.

Cor
 
Cor,
You say, "unfortunately" VB.NET does not have any escaped characters

Others like me say "fortunately".
Umm... My message is stating: its unfortunate for John.
It is just that we have another opinion, where I respect yours of course.
Do we?

As I've stated in other posts I normally use ChrW in constants...

So I don't see a big need for escape characters either. Granted as you
suggest, you need to know what the character codes are that you want to use,
for displayable ones you can use the Character Map Windows Accessory. For
control chars, such as Tab & NewLine you need to know what they are or use
ControlChars...

FWIW: For me ControlChars.Tab says a lot more then "\t". Plus I don't need
to worry about when to use @ before the string. For example @"\t" when I
literally want "\t" in the string, not a string with the ControlChars.Tab in
it...

Thanks for the clarification!
Jay
 
Back
Top