Testing for End of string

  • Thread starter Thread starter Eric A. Johnson
  • Start date Start date
E

Eric A. Johnson

Hi all,

I'm trying to test for the end of a string. I know that within C++,
it's '\0'; how do I test for it within VB.NET?

Thanks,
Eric
 
Eric said:
I'm trying to test for the end of a string. I know that within C++,
it's '\0'; how do I test for it within VB.NET?

Are you testing character by character? How are you reading the
string? To my knowledge, VB strings are terminated by any particular
character. Can't you compare your position in the string with the
string length? What are you trying to do?
 
Eric said:
Hi all,

I'm trying to test for the end of a string. I know that within C++,
it's '\0'; how do I test for it within VB.NET?

Strings in .NET are a proper type, rather than just being arrays of
char, as they are in C and C++. Therefore without further context it
doesn't really make sense to talk about 'testing for the end of a
string' - if you have a String, you have a String. What are you trying
to do?
 
Eric,
In addition to the other comments.

| I'm trying to test for the end of a string. I know that within C++,
| it's '\0'; how do I test for it within VB.NET?
Strings in VB.NET are fixed length strings. The String.Length member
indicates where the end of the string is.

In fact you will receive an exception if you attempt to access a character
with an index less then 0 or greater or equal the length.

This also means that *ALL* values of System.Char include '\0' are valid
characters for a String!

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net


| Hi all,
|
| I'm trying to test for the end of a string. I know that within C++,
| it's '\0'; how do I test for it within VB.NET?
|
| Thanks,
| Eric
|
|
 
You don't need to test for the end because the length of the string is
provided. .NET strings do not have a terminating character. All the
characters in the string's character array are valid.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Eric A. Johnson said:
I'm trying to test for the end of a string. I know that within C++,
it's '\0'; how do I test for it within VB.NET?

In addition to the other replies: You can determine the length of a string
by either checking its 'Length' property or using VB.NET's 'Len' function.
 
While .NET strings do not have a terminating character as such, the
nullchar will act like one if the string is displayed. Like this...


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click

Dim tstr As System.String = "ABCDEFGHJKLMN"
Dim newchar As Char
Dim bbyte() As Byte = {0}
newchar = System.Text.Encoding.ASCII.GetChars(bbyte)(0)
tstr = tstr.Replace("D", newchar)
System.Windows.Forms.MessageBox.Show(tstr)
-->> shows "ABC"
System.Windows.Forms.MessageBox.Show(tstr.Length.ToString)
-->> shows "13"

System.Windows.Forms.MessageBox.Show(tstr.IndexOf(newchar).ToString)
-->> shows "3"
End Sub

So while the string itself will hold the extra characters, the normal
way of showing it will not display past the nullchar.

The above code is a bit of a stretch to prove a point, but it can
happen while working with some API's that use string buffers passed
byref, communications when you have a buffer to fill and you only get
a partial message, etc.

You need to find where the end of the string that you want is located
and that is where you would use String.IndexOf

HTH,
Barry






You don't need to test for the end because the length of the string is
provided. .NET strings do not have a terminating character. All the
characters in the string's character array are valid.

bceggersATcomcastDOTnet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top