HomeSearch

VB.NET ToCharArray Function

Use the ToCharArray Function, which converts a String to a Char array.
ToCharArray converts a String to a Char array. It returns a Char array with a number of elements equal to the Length of the String. With this Char array, we change the String data efficiently, without again copying it.StringsArray
Example. First, this program declares and assigns a string that points to the literal "Perls". Then, the ToCharArray function is called on the String variable. Finally, the For-Each loop enumerates all the characters in the array.

Note: You can see that the character array contains the same five characters as the original string.

For Each, For
VB.NET program that uses ToCharArray function Module Module1 Sub Main() ' String input value. Dim value As String = "Perls" ' Call ToCharArray function. Dim array() As Char = value.ToCharArray ' Loop over Char array. For Each element As Char In array Console.WriteLine(element) Next End Sub End Module Output P e r l s
Discussion. A main reason you will want to use Char arrays based on your Strings in your VB.NET programs is that Char arrays are mutable and String instances are not. This can improve performance.

Note: You can change the individual characters in a Char array without copying the entire set of characters again.

Char Array
Summary. ToCharArray converts strings to their equivalent Char arrays. This enables you to change characters without having to copy strings. You cannot cast the String to a Char array. You must convert it—the best way is usually with ToCharArray.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls