C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: Third, the New Char() syntax is used with an array initializer. This is also a short syntax form.
Note: All of the arrays have identical contents when examined during execution.
VB.NET program that uses char arrays
Module Module1
Sub Main()
' Use the quick syntax.
Dim array1() As Char = {"s", "a", "m"}
' Use the long syntax.
Dim array2(2) As Char
array2(0) = "s"
array2(1) = "a"
array2(2) = "m"
' Another syntax.
Dim array3() As Char = New Char() {"s", "a", "m"}
' Display lengths.
Console.WriteLine(array1.Length)
Console.WriteLine(array2.Length)
Console.WriteLine(array3.Length)
End Sub
End Module
Output
3
3
3
And: The other approach you could use, which is slower, is the StringBuilder type and its Append function.
VB.NET program that assigns into char array
Module Module1
Sub Main()
' Allocate array of 100 characters.
Dim array(100) As Char
' Fill array with a character.
For index As Integer = 0 To 100
array(index) = "-"
Next
' Write some characters.
Console.WriteLine(array(0))
Console.WriteLine(array(100))
End Sub
End Module
Output
-
-
VB.NET program that uses ToCharArray
Module Module1
Sub Main()
Dim value As String = "test"
' Get char array from string.
Dim array() As Char = value.ToCharArray()
' Loop and print the chars.
For Each element As Char In array
Console.WriteLine("CHAR: {0}", element)
Next
End Sub
End Module
Output
CHAR: t
CHAR: e
CHAR: s
CHAR: t