I\'m having serious problems with string-handling. As my problems are rather hard to describe, I will start with some demo code reproducing them:
Dim s1 As S
I changed the variable names for clarity:
Dim myChars(30) As Char
myChars(0) = "h"c ' cannot convert string to char
myChars(1) = "i"c ' under option strict (narrowing)
Dim myStrA As New String(myChars)
Dim myStrB As String = CStr(myChars)
The short answer is this:
Under the hood, strings are character arrays. The last 2 lines both create a string one using NET code, the other a VB function. The thing is that, although the array has 31 elements, only 2 were initialized:
The rest are null/Nothing, which for a Char means Chr(0) or NUL. Since NUL is used to mark the end of a String, only the characters up to that NUL will print in the Console, MessageBox etc. Text appended to the string will not display either.
Concepts
Since the strings above are created directly from a char array, the length is that of the original array. The Nul is a valid char so they get added to the string:
Console.WriteLine(myStrA.Length) ' == 31
So, why doesn't Trim remove the nul characters? MSDN (and Intellisense) tells us:
[Trim] Removes all leading and trailing white-space characters from the current String object.
The trailing null/Chr(0) characters are not white-space like Tab, Lf, Cr or Space, but is a control character.
However, String.Trim has an overload which allows you to specify the characters to remove:
myStrA = myStrA.Trim(Convert.ToChar(0))
' using VB namespace constant
myStrA = myStrA.Trim( Microsoft.VisualBasic.ControlChars.NullChar)
You can specify multiple chars:
' nuls and spaces:
myStrA = myStrA.Trim(Convert.ToChar(0), " "c)
Strings can be indexed / iterated as a char array:
For n As Int32 = 0 To myStrA.Length
Console.Write("{0} is '{1}'", n, myStrA(n)) ' or myStrA.Chars(n)
Next
0 is 'h'
1 is 'i'
2 is '
(The output window will not even print the trailing CRLF.) You cannot change the string's char array to change the string data however:
myStrA(2) = "!"c
This will not compile because they are read-only.
See also:
ASCII table