Visual Basic Using String ToCharArray

In Visual Basic programming language, if we want to convert a string type variable to a Unicode character array, we use the ToCharArray() method.

Let's examine the example below..

Module ModuleTest Sub Main() Dim myString As String = "yazilimders" Dim chars As Char() = myString.ToCharArray() Console.WriteLine("yazilimders") Console.Write("CharArray :") For i As Integer = 0 To chars.Length - 1 Console.Write(" " & chars(i)) Next Console.ReadLine() End Sub End Module

With the "ToCharArray()" method, our yazilimders variable has turned into a char array with 11 elements. All characters in the String have become an element of the array.

Another way to use the ToCharArray() method is to give the starting index and length as parameters.

Below is an example of this usage.

Module ModuleTest Sub Main() Dim myString As String = "yazilimders" Dim chars As Char() = myString.ToCharArray(1, 3) Console.WriteLine("yazilimders") Console.Write("CharArray :") For i As Integer = 0 To chars.Length - 1 Console.Write(" " & chars(i)) Next Console.ReadLine() End Sub End Module

Our string value "yazimders" is requested to be converted to char array type as ToCharArray(1,3). In other words, only 3 characters from the 1st index (from the 2nd character) will be converted to a string, respectively.

As a result, an array of "a", "z", "i" elements is created..



You May Interest

Visual Basic How To Find The Average Of 10 Numbers Using A While ...

Visual Basic Reverse Array

Visual Basic Remove Element from Array

Visual Basic String Using StartsWith

Visual Basic Get Now