ARTICLE

Convert String to Char () array in vb.net

Posted by Sapna Articles | Visual Basic Language July 05, 2011
You can use overloaded ToCharArray method of string object to convert String to Char array.
 
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor