Conversion of String into Character array using VB.NET

You can use overloaded ToCharArray method of string object to convert String to Char array.
  • 6244

In this articles I am going to convert String to Char () array. You can use overloaded ToCharArray method of string object. You can easily buffer character array from a string and its return a new Char () array. I have used different methods and different way to use in below code.

img.gif

First method is simple to convert string to char array use ToCharArray method of string, ToCharArray method receive a character  array from the contents of a string and second I used string reader class that to reads from the specified string.

Code

Imports System.IO
Module Module1
    Sub Main()
        Dim str As [String] = "hello friends"
        'String to char array
        Dim chArray As Char() = str.ToCharArray()
        Console.Write(chArray(0))
        Console.Write(chArray(1))
        Console.Write(chArray(2))
        Console.Write(chArray(3))
        Console.Write(chArray(4))
        Console.Write(chArray(5))
        Console.Write(chArray(6))
        Console.Write(chArray(7))
        Console.Write(chArray(8))
        Console.Write(chArray(9))
        Console.Write(chArray(10))
        Console.Write(chArray(11))
        Console.Write(chArray(12))
        Console.WriteLine()
        Console.WriteLine()

        Dim str1 As New [String](chArray)
        Console.Write(str1)
        Console.WriteLine()
        Console.WriteLine()

        Dim str2 As String = "Hello World"
        Console.Write(str2(0))
        Console.WriteLine()
        Console.WriteLine()

        Dim sr As New StringReader(str)
        sr.Read(chArray, 0, 11)
        Console.WriteLine(chArray)
        sr.Close()
        Console.WriteLine()
        Console.WriteLine()

       ' use for loop
        For i As Integer = 0 To str.Length - 1
            ' Get character from array.
            Dim length As Char = str(i)
            Console.Write(length)
        Next
        Console.WriteLine()
        Console.WriteLine()

        'using foreach
        For Each c As Char In str
            Console.Write(c)
        Next
        Console.ReadLine()
    End Sub
End Module

Output
 
output.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.