Home
VB.NET
String ToCharArray Function
Updated Mar 25, 2022
Dot Net Perls
ToCharArray. This converts a VB.NET String to a Char array. It returns a Char array with a number of elements equal to the Length of the String.
String Length
With this Char array, we can change the String data efficiently, without copying it. It is possible to convert the char array back into a String as well.
String Constructor
First, this program declares and assigns a string that points to the literal "abcd". Then the ToCharArray function is called on the String variable.
Finally The For-Each loop enumerates all the characters in the array. We print each element to the console.
For
Note You can see that the character array contains the same characters as the original string.
Module Module1 Sub Main() ' String input value. Dim value As String = "abcd" ' 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
a b c d
A discussion. Char arrays are mutable and String instances are not. This means we can modify individual characters without changing the rest of the string. This can improve performance.
Note You can change the individual characters in a Char array without copying the entire set of characters again.
A summary. ToCharArray converts strings to their equivalent Char arrays. You cannot cast the String to a Char array. You must convert it—the best way is usually with ToCharArray.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 25, 2022 (image).
Home
Changes
© 2007-2025 Sam Allen